简体   繁体   English

来自带有条件的融化数据帧的同一层中具有ggplot2的多个箱形图

[英]Multiple boxplots with ggplot2 in the same layer from a melted data frame with a condition

I have some data here [in a .txt file] which I read into a data frame, [txt文件中]有一些数据这些数据已读入数据框,

mydf <- read.table("data.txt", header=T,sep="\t")

I melt this data frame next using the following piece of code, 接下来,我使用以下代码融化该数据帧,

df_mlt <-melt(mydf, id=names(mydf)[1], variable = "cols")

Now I would like to plot this data as a boxplot displaying only values of x>0 , so for this I use the following code, 现在,我想将此数据绘制为仅显示x>0值的箱线图,因此我使用以下代码,

plt_bx <-   ggplot(df_mlt, aes(x=ID1,y=value>0, color=cols))+geom_boxplot()

But the resulting plot looks like the following, 但是结果图如下所示,

箱线图的输出

However what I need is to only display positive values of x as individual box plots in the same plot layer. 但是,我需要的是在同一绘图图层中仅将x的正值显示为单个箱形图。 Could someone please suggest what I need to change in the above code to get the proper output, Thanks. 有人可以建议我需要在上述代码中进行更改以获取正确的输出,谢谢。

plt_bx <- ggplot(subset(df_mlt, value > 0), aes(x=ID1,y=value, color=cols)) + geom_boxplot()

You need to subset your data frame to remove the undesirable values. 您需要对数据框进行子集删除,以消除不想要的值。 Right now you're plotting value > 0 , which is either TRUE or FALSE, instead of the boxplot of only the values that are greater than 0. 现在,您正在绘制value > 0 ,即TRUE或FALSE,而不是仅绘制value > 0的值的箱形图。

Based on @BrodieG suggestions, the following piece of code yields a plot as below, 根据@BrodieG的建议,以下代码段生成如下图:

plt_bx <- ggplot(subset(df_mlt, value > 0), aes(x=ID1,y=value,group=ID1)) + 
  geom_boxplot(aes(color=ID1),outlier.colour="orangered", outlier.size=3) +
  scale_y_log10(labels = trans_format("log10", math_format(10^.x))) +
  theme_bw() +
  theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+
  theme(axis.text=element_text(size=26)) +
  theme(axis.title=element_text(size=22,face="bold")) +
  labs(x = "x", y = "y", colour="Values") +
  annotation_logticks(sides = "rl")
plt_bx

在此处输入图片说明

I improved my answer, the outline of the boxplot would display different colors if color in the aes is assigned as a factor of the id from the melted data frame. 我改善了答案,如果将es中的颜色分配为融化数据帧中id的因子,则箱形图的轮廓将显示不同的颜色。 ie, geom_boxplot(aes(color=factor(ID1))) geom_boxplot(aes(color=factor(ID1)))

The following code results in a plot as below, 以下代码生成如下图:

plt <- ggplot(subset(df_mlt, value > 0), aes(x=ID1,y=value)) + 
  geom_boxplot(aes(color=factor(ID1))) +
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +
  theme_bw() +
  theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+
  theme(axis.text=element_text(size=20)) +
  theme(axis.title=element_text(size=20,face="bold")) +
  labs(x = "x", y = "y",colour="legend" ) +
  annotation_logticks(sides = "rl") +
  theme(panel.grid.minor = element_blank()) +
  guides(title.hjust=0.5) +
  theme(plot.margin=unit(c(0,1,0,0),"mm")) 
plt

输出图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM