'Ordering bars in ggplot with data from two two data sets

So i have data in two separate excel sheets (CSV files) that i am inputting into R. The data in one sheet is plotted as bars and the data in the other sheet is plotted as dots. How can i make it so i can dictate the order of all the data samples on the x axis. I used this code to order the bars:

data7$Virus.hpi <- factor(data7$Virus.hpi, levels = c("BTV-8 0hpsi", "BTV-8 72hpsi", "BTV-1 0hpsi", "BTV-1 72hpsi"))

 ggplot(data=data7, aes(x = data7$Virus.hpi, y=ct, fill=Virus.hpi)) + 
scale_fill_manual(values = c("BTV-8 0hpsi" = "purple", "BTV-8 72hpsi" = "purple", "BTV-1 0hpsi" = "blue", "BTV-1 72hpsi" = "blue")) + 
stat_summary(fun = mean, geom = "bar") + 
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3) +
 scale_y_continuous(breaks = seq(0, 40, 10), labels = rev(seq(0, 40, 10)))

Which gave a bar chart with the bars in the order i wanted them, however when i added in my code for the dot plot the bars reverted back to the orginal order.

This was the code with the dot plot:

data4$Virus.hpi <- factor(data4$Virus.hpi, levels = c("BTV-8 0hpi", "BTV-8 72hpi", "BTV-1 0hpi", "BTV-1 72hpi"))

ggplot(data=data7, aes(x = data7$Virus.hpi, y=ct, fill=Virus.hpi)) + 
scale_fill_manual(values = c("BTV-8 0hpsi" = "purple", "BTV-8 72hpsi" = "purple", "BTV-1 0hpsi" = "blue", "BTV-1 72hpsi" = "blue")) + 
stat_summary(fun = mean, geom = "bar") + 
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3) + 
geom_point(data=data4, aes(x = data4$Virus.hpi, y=ct, fill=Virus.hpi)) + 
scale_y_continuous(breaks = seq(0, 40, 10), labels = rev(seq(0, 40, 10)))

Thanks

dput(data7)

structure(list(Virus.hpi = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("BTV-8 0hpsi", "BTV-8 72hpsi", "BTV-1 0hpsi", 
"BTV-1 72hpsi"), class = "factor"), X = c(30.78, 29.15, 30.07, 
30.03, 28.77, 29.8, 24.59, 25.86, 28.69, 23.88, 23.07, 26.7, 
25.26, 16.07, 24.31, 23.86, 23.08, 23.8, 13.07, 15.67, 17.54, 
16.79, 17.23, 15.41), ct = c(9.22, 10.85, 9.93, 9.97, 11.23, 
10.2, 15.41, 14.14, 11.31, 16.12, 16.93, 13.3, 14.74, 23.93, 
15.69, 16.14, 16.92, 16.2, 26.93, 24.33, 22.46, 23.21, 22.77, 
24.59)), row.names = c(NA, -24L), class = "data.frame")

dput(data4)

structure(list(Virus.hpi = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L), .Label = c("BTV-8 0hpi", "BTV-8 72hpi", 
"BTV-1 0hpi", "BTV-1 72hpi"), class = "factor"), X = c(32.11, 
33.12, 32.2, 14.87, 17.65, 21.5, 29.33, 30.33, 29.7, 16.42, 17.9, 
14.6), ct = c(7.89, 6.88, 7.8, 25.13, 22.35, 18.5, 10.67, 9.67, 
10.3, 23.58, 22.1, 25.4)), row.names = c(NA, -12L), class = "data.frame")


Solution 1:[1]

The problem is that the factor levels in data4 are different from those in data7. In data7, the labels all have an extra "s" in them, for example the first level in data7 is "BTV-8 0hpsi", whereas in data4 it is "BTV-8 0hpi". ggplot is putting all of these 8 levels in the x axis, and since it has had to combine two vectors of factor levels, it has had to default to alphabetical order.

One possible solution is to simply remove the "s" in the labels of data7

data7$Virus.hpi <- sub("s", "", as.character(data7$Virus.hpi))
data7$Virus.hpi <- factor(data7$Virus.hpi, levels = levels(data4$Virus.hpi))

Now the plot retains the specified colors

ggplot(data7, aes(Virus.hpi, ct, fill = Virus.hpi)) + 
  scale_fill_manual(values = c("BTV-8 0hpi" = "purple", "BTV-8 72hpi" = "purple", 
                             "BTV-1 0hpi" = "blue", "BTV-1 72hpi" = "blue")) + 
  stat_summary(fun = mean, geom = "bar") + 
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3) +
  geom_point(data = data4) + 
  scale_y_continuous(breaks = seq(0, 40, 10), labels = rev(seq(0, 40, 10)))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Allan Cameron