[R] Just to you
@vi@e@gross m@iii@g oii gm@ii@com
@vi@e@gross m@iii@g oii gm@ii@com
Thu Jul 13 05:20:35 CEST 2023
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)
> temp
Time 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)
> temp
Time 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")
> temp
Newname1 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 using r-project.org> On Behalf Of Sorkin, John
Sent: Wednesday, July 12, 2023 9:17 PM
To: r-help using r-project.org (r-help using r-project.org) <r-help using 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 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.
More information about the R-help
mailing list