0

I have a date frame (df), with 2 columns: One numerical and one as.factor() with three levels:

  • Pre
  • Post
  • Blank

I want to make a barplot() with each factor colored to it's respective group (easy), and change the order of the plot so each factor appears next to each other (this is where I'm stuck).

I followed the same logic as I would with a boxplot(), but it does not appear to work the same. I also tried following examples from several stackoverflow threads, including (but not limited to) this one:

But still can't get it to work.

Here is what I've tried, and it works with the boxplot function quite well:

df <- read.table("https://pastebin.com/raw/zaETq28M", header = T)

df$Treatment <- as.factor(df$Treatment)

levels(df$Treatment) # note: I would like to display order to be: Pre, Post, then Blank.

df$Treatment <- ordered(df$Treatment, levels = c("Pre","Post","Blank")) # set to the right order

barplot(df$Cq,names.arg = df$Treatment ,col = df$Treatment, ylim=c(0,30), main = "Not the right order bar plot", cex.main=2)

In total, I should have 66 individual bars (which I do), but somehow, the order of the graph is not what I set, and the groups are still separated. How can I simply get 3 distinct groups? Meaning, first show all "Pre", then all "post", followed by "blank"

General questions for future posts:

  1. How to get a get my graphs to be displayed on Stackoverflow when I post a question? For some reason, my posts never include my graphs.
  2. Also, any kind suggestion on using color blind pallet would be great, but I can just do this manually if needed. Just curious if there is an automatic way of doing it, so I do not need to set it manually in all my graphs

Thank you for your help

Andy
  • 413
  • 2
  • 15

1 Answers1

1

Do you mean this? First the Pre, then Post then blank. Within each group order is preserved. Legend added with blank == No Treatment.

df <- read.table("https://pastebin.com/raw/zaETq28M", header = T)

df_Pre <- df[which(df$Treatment == 'Pre'),]
df_Post <- df[which(df$Treatment == 'Post'),]
df_Blank <- df[which(df$Treatment == 'Blank'),]
ddf <- rbind(df_Pre, df_Post, df_Blank)
ddf$color <- c(rep('blue', nrow(df_Pre)), rep('red', nrow(df_Post)), rep('magenta', nrow(df_Blank)))
barplot(ddf$Cq, col = ddf$color, names = rownames(ddf))
legend("bottomleft", 
       legend = c("Pre-Treatmen", "Post-Treatment", 'No Treatment'), 
       fill = c("darkblue", "red","magenta"))

MarBlo
  • 4,195
  • 1
  • 13
  • 27
  • Hi. Thanks for your time. No, this is a frequency distribution table of my groups. My y-axis should be the variable "cq", and the x-axis should have 3 groups: Pre, Post, and Blank if you run the code ```table(df$Treatment), you'll see I have 66 total data points, meaning 66 individual barplots. Does this help? Try running my code as is, and you will see what I mean, just the grouping is wrong – Andy Jan 07 '21 at 13:18
  • I updated my question to hopefully clarify my point – Andy Jan 07 '21 at 13:32
  • @Andy, I hope I understood now - please, see new edit. – MarBlo Jan 07 '21 at 15:22
  • 1
    Isn't that cool! Many thanks for spending time on this :) – Andy Jan 07 '21 at 15:51
  • Can you think of a way to have the x-axis line, with tick marks, under each bar, but no numbers? I first added ```xaxt="n"``` to the barplot command, and then I tried this code: ```xtick<-seq(0, 66, by=1) axis(1, at=xtick, labels = F)```but was not correct. – Andy Jan 07 '21 at 16:45
  • 1
    Try this `barplot(ddf$Cq, col = ddf$color, names = rownames(ddf), axis.lty = 1, names.arg = rep('', nrow(ddf)))` – MarBlo Jan 07 '21 at 17:14
  • Hi @marblo : Yes it works. It's strange that it isn't lined up properly, the tick marks are not in the center of the bars, but thats OK. I tried a few coding options to get around the issue, but I decided I just will leave it without an x-axis, But even just a black line without ticks would be nicer than no line, I just can't figure it out. – Andy Jan 08 '21 at 10:12