ggplot绘图横轴分组问题

2020-11-04  本文已影响0人  谢京合

之前在用ggplot画图的时候还没发现这个问题
今天出现就记录下

先前的数据格式如下:


image.png

画图的时候一般是按照lable那一列来绘制的。
绘图命令

p1 <- ggplot(input,aes(x=lable, y=log(value)))+
stat_boxplot(geom = "errorbar",width=0.1,aes(color="black"))+
geom_boxplot(size=0.5,fill="white",outlier.fill="white",outlier.color="white")+
geom_jitter(aes(fill=lable),width =0.3,shape=21,size=2.5)+
scale_fill_manual(values = c("blue", "red"))+
scale_color_manual(values=c("black","white"))+
theme_bw()+
theme(legend.position="none", #不需要图例
        axis.text.x=element_text(colour="black",family="Times",size=14), #设置x轴刻度标签的字体属性
        axis.text.y=element_text(family="Times",size=14,face="plain"), #设置x轴刻度标签的字体属性
        axis.title.y=element_text(family="Times",size = 14,face="plain"), #设置y轴的标题的字体属性
        axis.title.x=element_text(family="Times",size = 14,face="plain"), #设置x轴的标题的字体属性
        plot.title = element_text(family="Times",size=15,hjust = 0.5), #设置总标题的字体属性
        panel.grid.major = element_blank(), #不显示网格线
        panel.grid.minor = element_blank())+
  ylab("Expression of log(IT1)")+
  xlab("TCGA database")

看起来还行。

但是今天的数据格式如下图:


image.png

我要按照copyscore这里列来绘制。

p1 <- ggplot(f,aes(x=copyScore, y=log(value)))+
geom_boxplot(size=0.5,fill="orange",outlier.fill="red",outlier.color="white")+
theme_bw()

这里就会只得到一个box。


image.png

所以,要么就是在ggplot的时候稍作修改。

p1 <- ggplot(input,aes(group=copyscore, y=log(value)))+
geom_boxplot(size=0.5,fill="white",outlier.fill="orange",outlier.color="red")

但是得到的横轴的坐标也不对,我的数据里面是(-1,0,1),但是图上面是(0.2,0,0.2)


image.png

就很奔溃。

所以这个应该是ggplot不识别数字。
把你需要分组的一列转换成str就完美解决问题了。


image.png
上一篇下一篇

猜你喜欢

热点阅读