[R] ggplot: Can plot graphs with points, can't plot graph with points and line
John Kane
jrkr|de@u @end|ng |rom gm@||@com
Thu Jul 13 14:30:50 CEST 2023
Hi John,
This should do what you want. I've changed your data.frame name for my own
convenience to "dat1".
###===========================================
dat1 <- data.frame(
Time = c("Age.25","Age.35","Age.45","Age.55"),
Medians = c(128.25,148.75,158.5,168.75)
)
# create segments data.frame
dat2 <- data.frame(x = dat1$Time[1:3], xend = dat1$Time[2:4],
y = dat1$Medians[1:3], yend = dat1$Medians[2:4])
p1 <- ggplot(dat1 ,aes(x = Time, y = Medians)) +
geom_point()
p1 + geom_segment( x = "Age.25", y = 128.25, xend = "Age.35", yend =
148.75) +
geom_segment( x = "Age.35", y = 148.75, xend = "Age.45", yend = 158.5) +
geom_segment( x = "Age.45", y = 158.5, xend = "Age.55", yend = 168.55)
# This solution shamelessly stolen from
##
https://stackoverflow.com/questions/62536499/how-to-draw-multiple-line-segment-in-ggplot
p1 + geom_segment(
data = dat2,
mapping = aes(x=x, y=y, xend=xend, yend=yend),
inherit.aes = FALSE
)
On Thu, 13 Jul 2023 at 01:11, Jim Lemon <drjimlemon using gmail.com> wrote:
> 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 using 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 using 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.
>
> ______________________________________________
> R-help using 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.
>
--
John Kane
Kingston ON Canada
[[alternative HTML version deleted]]
More information about the R-help
mailing list