Question

I have some data here [in a .txt file] which I read into a data frame,

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,

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

But the resulting plot looks like the following,

Output of the boxplot

However what I need is to only display positive values of x as individual box plots in the same plot layer. Could someone please suggest what I need to change in the above code to get the proper output, Thanks.

Was it helpful?

Solution 3

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. i.e., 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

Output plot

OTHER TIPS

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.

Based on @BrodieG suggestions, the following piece of code yields a plot as below,

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

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top