Luigi Marongiu
2023-Jul-02 05:31 UTC
[R] How to plot both lines and points by group on ggplot2
Thank you that is exactly it! The problem was to connect each point of the series 'Conc' with a line. Best regards Luigi On Sat, Jul 1, 2023 at 8:33?PM Chris Evans <chrishold at psyctc.org> wrote:> > > On 01/07/2023 19:20, Luigi Marongiu wrote: > > Hello, > > I have a dataframe with measurements stratified by the concentration > > of a certain substance. I would like to plot the points of the > > measures and connect the points within each series of concentrations. > > When I launch ggplot2 I get the error > > ``` > > geom_path: Each group consists of only one observation. Do you need to > > adjust the > > group aesthetic? > > ``` > > and no lines are drawn. > > Where am I going wrong? > > Thank you > > Luigi > > > > ``` > > df = data.frame(Conc = c(rep(1, 3), rep(2, 3), rep(5, 3)), > > Time = rep(1:3, 3), > > Value = c(0.91, 0.67, 0.71, 0.91, 0.65, 0.74, 0.95, 0.67, 0.67)) > > df$Time <- as.factor(df$Time) > > levels(df$Time) = c(1, 4, 24) > > df$Conc <- as.factor(df$Conc) > > levels(df$Conc) = c(1, 2, 5) > > library(ggplot2) > > ggplot(df, aes(x=Time, y=Value, colour=Conc)) + > > geom_point(size=6) + > > geom_line(aes(x=Time, y=Value, colour=Conc)) + > > scale_colour_manual(values = c("darkslategray3", "darkslategray4", > > "deepskyblue4")) + > > ggtitle("Working example") + > > xlab(expression(bold("Time (h)"))) + > > ylab(expression(bold("Concentration (mM)"))) > > ``` > > If I understand what you want then I think all you need is to add "group > = Conc". That's what that message is telling you. > > df = data.frame(Conc = c(rep(1, 3), rep(2, 3), rep(5, 3)), > Time = rep(1:3, 3), > Value = c(0.91, 0.67, 0.71, 0.91, 0.65, 0.74, 0.95, > 0.67, 0.67)) > df$Time <- as.factor(df$Time) > levels(df$Time) = c(1, 4, 24) > df$Conc <- as.factor(df$Conc) > levels(df$Conc) = c(1, 2, 5) > library(ggplot2) > ggplot(df, aes(x=Time, y=Value, colour=Conc, group = Conc)) + > geom_point(size=6) + > geom_line(aes(x=Time, y=Value, colour=Conc)) + > scale_colour_manual(values = c("darkslategray3", "darkslategray4", > "deepskyblue4")) + > ggtitle("Working example") + > xlab(expression(bold("Time (h)"))) + > ylab(expression(bold("Concentration (mM)"))) > > Very best, > > > Chris > > -- > Chris Evans (he/him) > Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, > University of Roehampton, London, UK. > Work web site: https://www.psyctc.org/psyctc/ > CORE site: http://www.coresystemtrust.org.uk/ > Personal site: https://www.psyctc.org/pelerinage2016/ > Emeetings (Thursdays): > https://www.psyctc.org/psyctc/booking-meetings-with-me/ > (Beware: French time, generally an hour ahead of UK) > <https://ombook.psyctc.org/book>-- Best regards, Luigi