Luigi Marongiu
2023-Jul-01 18:20 UTC
[R] How to plot both lines and points by group on ggplot2
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)"))) ``` -- Best regards, Luigi
Bert Gunter
2023-Jul-01 18:43 UTC
[R] How to plot both lines and points by group on ggplot2
Can your post a few lines of your data frame (e.g. via dput(head(...))) to make it clearer. I found this statement confusing: "... 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." This might or might not be the source of your problems, but I think showing us (enough of) your data would help clarify things. I don't use ggplot, but someone who does might have a better chance of pinpointing the problem if you provide such data. Cheers, Bert On Sat, Jul 1, 2023 at 11:21?AM Luigi Marongiu <marongiu.luigi at gmail.com> 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)"))) > ``` > -- > Best regards, > Luigi > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Chris Evans
2023-Jul-01 18:48 UTC
[R] How to plot both lines and points by group on ggplot2
[Whoops, forgot that default reply-to is to the sender.] 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>
Rui Barradas
2023-Jul-01 19:24 UTC
[R] How to plot both lines and points by group on ggplot2
?s 19:20 de 01/07/2023, Luigi Marongiu escreveu:> 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)"))) > ```Hello, Here are two solutions. I have removed the redundant aes() from geom_line in both plots. 1. If you do not coerce Time to factor, the x axis will be continuous. The plot will be as expected but you wi have to include a scale_x_continuous to have the wanted labels. 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)) library(ggplot2) df$Conc <- factor(df$Conc, levels = c(1, 2, 5)) ggplot(df, aes(x=Time, y=Value, colour=Conc)) + geom_point(size=6) + geom_line() + scale_colour_manual(values = c("darkslategray3", "darkslategray4", "deepskyblue4")) + scale_x_continuous(breaks = 1:3, labels = c(1, 2, 24)) + ggtitle("Working example") + xlab(expression(bold("Time (h)"))) + ylab(expression(bold("Concentration (mM)"))) 2. Time is coerced to factor. Then, tell geom_line the data is grouped by Conc. This is probably the solution you should use. df$Time <- factor(df$Time, labels = c(1, 4, 24)) ggplot(df, aes(x=Time, y=Value, colour=Conc)) + geom_point(size=6) + geom_line(aes(group = Conc)) + scale_colour_manual(values = c("darkslategray3", "darkslategray4", "deepskyblue4")) + ggtitle("Working example") + xlab(expression(bold("Time (h)"))) + ylab(expression(bold("Concentration (mM)"))) Hope this helps, Rui Barradas