Sorkin, John
2023-Jul-13 01:17 UTC
[R] ggplot: Can plot graphs with points, can't plot graph with points and line
I am trying to plot four points, and join the points with lines. I can plot the points, but I can't plot the points and the line. I hope someone can help my with my ggplot code. # load ggplot2 if(!require(ggplot2)){install.packages("ggplot2")} library(ggplot2) # Create data Time <- c("Age.25","Age.35","Age.45","Age.55") Medians <-c(128.25,148.75,158.5,168.75) themedians <- matrix(data=cbind(Time,Medians),nrow=4,ncol=2) dimnames(themedians) <- list(NULL,c("Time","Median")) # Convert to dataframe the data format used by ggplot themedians <- data.frame(themedians) themedians # This plot works ggplot(themedians,aes(x=Time,y=Median))+ geom_point() # This plot does not work! ggplot(themedians,aes(x=Time,y=Median))+ geom_point()+ geom_line() Thank you, John
John, I am a tad puzzled at why your code does not work so I tried replicating it. Let me say you are not plotting what you think. When you plot points using characters, it LOOKS like it did something but not really. It labels four equally apart lines (when your data is not linear) and you are getting nosense. But when you try for lines, using otherwise valid code, it fails. Based on your earlier post, I sort of understood what you did but find it roundabout and not necessary. And you made all columns of type character! You had two perfectly good vectors of type character and floating point. You eventually wanted a data.frame or the like. I assume your code is an example of something more complex because normally code like this works fine:> temp <- data.frame(Time=Time, Median=Medians) > tempTime Median 1 Age.25 128.25 2 Age.35 148.75 3 Age.45 158.50 4 Age.55 168.75 Alternatively, these two lines let you make a data.frame with default names and rename it, skipping the matrix part as that nonsense makes all the columns character and you need floating point for a graph!> temp <- data.frame(Time, Medians) > tempTime Medians 1 Age.25 128.25 2 Age.35 148.75 3 Age.45 158.50 4 Age.55 168.75> colnames(temp) <- c("Newname1", "Newname2") > tempNewname1 Newname2 1 Age.25 128.25 2 Age.35 148.75 3 Age.45 158.50 4 Age.55 168.75 Now in your code using ggplot, as stated above it only looks like it works. Using my temp, the points are where they belong. Yes, it breaks when adding lines because the code is trying to group things as one axis is categorical. One solution is to tell it not to group: This works for me: ggplot(temp,aes(x=Time,y=Median))+ + geom_point()+ + geom_line(aes(group=NA)) I cannot attach a graph to messages on this forum but I suggest you modify your code to include this. Either do not use the matrix intermediate and keep your numbers as numbers, or convert the darn column back to numeric. And for now, try my ungrouping. Alternate suggestion, don't graph as above. Consider three columns and graph numbers versus numbers adding a label wherever you want: library(ggplot2) Time <- c(25, 35, 45, 55) Timelabels <- paste("age.", Time, sep="") Medians <-c(128.25,148.75,158.5,168.75) mydata=data.frame(Time=Time, Median=Medians) rownames(mydata) <- Timelabels ggplot(data=mydata, aes(x=Time, y=Medians)) + geom_line() + geom_point() +geom_text(label=rownames(mydata)) ggplot(data=mydata, aes(x=Time, y=Medians)) + geom_line() + geom_point(size=3, color="red") + geom_label(label=rownames(mydata),alpha=0.4) There are likely better ways but my point is it may be best to plot numbers against numbers. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Sorkin, John Sent: Wednesday, July 12, 2023 9:17 PM To: r-help at r-project.org (r-help at r-project.org) <r-help at r-project.org> Subject: [R] ggplot: Can plot graphs with points, can't plot graph with points and line I am trying to plot four points, and join the points with lines. I can plot the points, but I can't plot the points and the line. I hope someone can help my with my ggplot code. # load ggplot2 if(!require(ggplot2)){install.packages("ggplot2")} library(ggplot2) # Create data Time <- c("Age.25","Age.35","Age.45","Age.55") Medians <-c(128.25,148.75,158.5,168.75) themedians <- matrix(data=cbind(Time,Medians),nrow=4,ncol=2) dimnames(themedians) <- list(NULL,c("Time","Median")) # Convert to dataframe the data format used by ggplot themedians <- data.frame(themedians) themedians # This plot works ggplot(themedians,aes(x=Time,y=Median))+ geom_point() # This plot does not work! ggplot(themedians,aes(x=Time,y=Median))+ geom_point()+ geom_line() Thank you, John ______________________________________________ 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.
Jim Lemon
2023-Jul-13 05:10 UTC
[R] ggplot: Can plot graphs with points, can't plot graph with points and line
Hi John, I'm not sure how to do this with ggplot, but: Time<- c("Age.25","Age.35","Age.45","Age.55") Medians<-c(128.25,148.75,158.5,168.75)> is.character(Time)# [1] TRUE - thus it has no intrinsic numeric value to plot> is.numeric(Medians)# [1] TRUE # coerce Medians to factor and then plot against Time, but can't do point/line plot(as.factor(Time),Medians,type="p") # let R determine the x values (1:4) and omit the x-axis plot(Medians,type="b",xaxt="n") # add the x-axis with the "Time" labels axis(1,at=1:4,labels=Time) On Thu, Jul 13, 2023 at 11:18?AM Sorkin, John <jsorkin at som.umaryland.edu> wrote:> > I am trying to plot four points, and join the points with lines. I can plot the points, but I can't plot the points and the line. > I hope someone can help my with my ggplot code. > > # load ggplot2 > if(!require(ggplot2)){install.packages("ggplot2")} > library(ggplot2) > > # Create data > Time <- c("Age.25","Age.35","Age.45","Age.55") > Medians <-c(128.25,148.75,158.5,168.75) > themedians <- matrix(data=cbind(Time,Medians),nrow=4,ncol=2) > dimnames(themedians) <- list(NULL,c("Time","Median")) > # Convert to dataframe the data format used by ggplot > themedians <- data.frame(themedians) > themedians > > # This plot works > ggplot(themedians,aes(x=Time,y=Median))+ > geom_point() > # This plot does not work! > ggplot(themedians,aes(x=Time,y=Median))+ > geom_point()+ > geom_line() > > Thank you, > John > > ______________________________________________ > 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.