Arrange ggplot by "mean"

Hi there,

I am absolutely not experienced in using "r" and I am struggling with a very basic issue. I have a table with different fabric types (fabric). 4 different study participants (participant) were wearing them and exposed their arms to hungry mosquitoes. The number of mosquito bites were counted and are listed in the table (n). I plotted the fabric samples on the y-axis and the number of bites on the x-axis. The different study participants are marked in 4 different colours (points). I used a geom_point function and added the means with stat_summary. As it is now, the fabrics are sorted alphabetically, however, I want them sorted by the mean of mosquito bites. I don't get it done! Does somebody know an easy solution?

I added both the data table and the plot. This is the sripc I wrote so far:

ggplot(data = R_Table_1_Bites, aes(x = n, y = fabric, colour = participant)) + 
  geom_point(size = 5) +
  stat_summary(fun = mean, geom = "point", shape = 124, size = 5, colour = "black", fill = "black") +
  theme_light()+
  labs(colour="Study participant", group="Study participant", linetype="Study participant")
  

last_plot() + xlim(0, 7)
last_plot() + xlab("Number of bites")
last_plot() + ylab("Fabric sample")
last_plot() + theme(axis.text.x = element_text(size = 15))
last_plot() + theme(axis.text.y = element_text(size = 15))
last_plot() + theme(legend.text = element_text(size = 15))
last_plot() + theme(axis.title.x = element_text(size =15))
last_plot() + theme(axis.title.y = element_text(size =15))
last_plot() + theme(legend.title = element_text(size = 15, face="bold"))

Data in dput:

structure(list(fabric = c("19.423 x100", "19.425 x50", "Negative Control",
"19.423 x100", "19.424 x50", "Negative Control", "19.422 x100",
"19.424 x100", "19.424 x50", "19.425 x100", "Negative Control",
"19.422 x50", "19.423 x50", "19.424 x100", "19.425 x100", "19.425 x50",
"19.520 x100", "19.520 x50", "19.520 x50", "Negative Control",
"19.25 EN", "19.25 EN", "19.25 EN", "19.25 EN", "19.25 x100",
"19.25 x100", "19.25 x100", "19.25 x100", "19.25 x50", "19.25 x50",
"19.25 x50", "19.25 x50", "19.422 EN", "19.422 EN", "19.422 EN",
"19.422 EN", "19.422 x100", "19.422 x100", "19.422 x100", "19.422 x50",
"19.422 x50", "19.422 x50", "19.423 EN", "19.423 EN", "19.423 EN",
"19.423 EN", "19.423 x100", "19.423 x100", "19.423 x50", "19.423 x50",
"19.423 x50", "19.424 EN", "19.424 EN", "19.424 EN", "19.424 EN",
"19.424 x100", "19.424 x100", "19.424 x50", "19.424 x50", "19.425 EN",
"19.425 EN", "19.425 EN", "19.425 EN", "19.425 x100", "19.425 x100",
"19.425 x50", "19.425 x50", "19.520 EN", "19.520 EN", "19.520 EN",
"19.520 EN", "19.520 x100", "19.520 x100", "19.520 x100", "19.520 x50",
"19.520 x50"), endpoint = c("Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
"Bites", "Bites"), participant = c("Male 2", "Male 2", "Male 2",
"Female 2", "Male 2", "Male 1", "Male 2", "Female 1", "Female 1",
"Male 2", "Female 1", "Female 2", "Male 2", "Male 1", "Male 1",
"Male 1", "Male 2", "Male 1", "Female 2", "Female 2", "Female 2",
"Female 2", "Female 2", "Male 2", "Female 1", "Male 1", "Female 2",
"Male 2", "Female 1", "Male 1", "Female 2", "Male 2", "Female 1",
"Male 1", "Female 2", "Male 2", "Female 1", "Male 1", "Female 2",
"Female 1", "Male 1", "Male 2", "Female 1", "Male 1", "Female 2",
"Male 2", "Female 1", "Male 1", "Female 1", "Male 1", "Female 2",
"Female 1", "Male 1", "Female 2", "Male 2", "Female 2", "Male 2",
"Male 1", "Female 2", "Female 1", "Male 1", "Female 2", "Male 2",
"Female 1", "Female 2", "Female 1", "Female 2", "Female 1", "Male 1",
"Female 2", "Male 2", "Female 1", "Male 1", "Female 2", "Female 1",
"Male 2"), n = c(5, 5, 5, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
-76L), class = c("tbl_df", "tbl", "data.frame"))

Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

Hi there,

Thanks for your reply! I used the dput function to copy the data table in my initial request.

Is this what you are looking for?

library(ggplot2)
DF <- structure(list(fabric = c("19.423 x100", "19.425 x50", "Negative Control",
                            "19.423 x100", "19.424 x50", "Negative Control", "19.422 x100",
                            "19.424 x100", "19.424 x50", "19.425 x100", "Negative Control",
                            "19.422 x50", "19.423 x50", "19.424 x100", "19.425 x100", "19.425 x50",
                            "19.520 x100", "19.520 x50", "19.520 x50", "Negative Control",
                            "19.25 EN", "19.25 EN", "19.25 EN", "19.25 EN", "19.25 x100",
                            "19.25 x100", "19.25 x100", "19.25 x100", "19.25 x50", "19.25 x50",
                            "19.25 x50", "19.25 x50", "19.422 EN", "19.422 EN", "19.422 EN",
                            "19.422 EN", "19.422 x100", "19.422 x100", "19.422 x100", "19.422 x50",
                            "19.422 x50", "19.422 x50", "19.423 EN", "19.423 EN", "19.423 EN",
                            "19.423 EN", "19.423 x100", "19.423 x100", "19.423 x50", "19.423 x50",
                            "19.423 x50", "19.424 EN", "19.424 EN", "19.424 EN", "19.424 EN",
                            "19.424 x100", "19.424 x100", "19.424 x50", "19.424 x50", "19.425 EN",
                            "19.425 EN", "19.425 EN", "19.425 EN", "19.425 x100", "19.425 x100",
                            "19.425 x50", "19.425 x50", "19.520 EN", "19.520 EN", "19.520 EN",
                            "19.520 EN", "19.520 x100", "19.520 x100", "19.520 x100", "19.520 x50",
                            "19.520 x50"), endpoint = c("Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites", "Bites", "Bites", "Bites", "Bites", "Bites",
                                                        "Bites", "Bites"), participant = c("Male 2", "Male 2", "Male 2",
                                                                                           "Female 2", "Male 2", "Male 1", "Male 2", "Female 1", "Female 1",
                                                                                           "Male 2", "Female 1", "Female 2", "Male 2", "Male 1", "Male 1",
                                                                                           "Male 1", "Male 2", "Male 1", "Female 2", "Female 2", "Female 2",
                                                                                           "Female 2", "Female 2", "Male 2", "Female 1", "Male 1", "Female 2",
                                                                                           "Male 2", "Female 1", "Male 1", "Female 2", "Male 2", "Female 1",
                                                                                           "Male 1", "Female 2", "Male 2", "Female 1", "Male 1", "Female 2",
                                                                                           "Female 1", "Male 1", "Male 2", "Female 1", "Male 1", "Female 2",
                                                                                           "Male 2", "Female 1", "Male 1", "Female 1", "Male 1", "Female 2",
                                                                                           "Female 1", "Male 1", "Female 2", "Male 2", "Female 2", "Male 2",
                                                                                           "Male 1", "Female 2", "Female 1", "Male 1", "Female 2", "Male 2",
                                                                                           "Female 1", "Female 2", "Female 1", "Female 2", "Female 1", "Male 1",
                                                                                           "Female 2", "Male 2", "Female 1", "Male 1", "Female 2", "Female 1",
                                                                                           "Male 2"), n = c(5, 5, 5, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1,
                                                                                                            1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                                                                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                                                                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
                                                                                                                                                                                     -76L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(data = DF, aes(x = n, y = reorder(fabric, n), colour = participant)) + 
  geom_point(size = 5) +
  stat_summary(fun = mean, geom = "point", shape = 124, size = 5, colour = "black", fill = "black") +
  theme_light()+
  labs(colour="Study participant", group="Study participant", linetype="Study participant")

Created on 2021-04-26 by the reprex package (v0.3.0)

This is exactly what I was looking for - thank you very much for this :pray: :handshake:!!!