Re: [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 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@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@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] Just to you
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 On Behalf Of Sorkin, John Sent: Wednesday, July 12, 2023 9:17 PM To: r-help@r-project.org (r-help@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@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@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] 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@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.
Re: [R] Variable and value labels
Anupam, Your question, even after looking at other messages, remains a bit unclear. What do you mean by "labels"? What you mean by variables and values and how is that related to factors? An example or two would be helpful so we can say more than PROBABLY. Otherwise, you risk having many people here waste lots of time sending answers to questions you did not ask. And why an insistence on not using packages? If you are doing something for a class, sure, you may need to use the basics you were supposedly taught in class or a textbook. Otherwise, a good set of packages makes code much easier to write and often more reliable. Realistically, quite a bit of what some call base R is actually packages deemed useful enough to be included at startup and that can change. If you are new to R, note you can attach arbitrary attributes to a variable and you can have things like named lists where some or all the items have names as an attribute. Factors are part of base R and are a completely different concept. You can use base R to get or set the base levels of a factor and many other things and there are advantages sometimes in using a vector in factor mode than plain but also sometimes disadvantages. If you ask a more specific and properly explained question, maybe we can help you. Specifically, please tell us how you plan on using your labels. As an example, if I make a named list like this: mylist <- list(pi=3.14, e=2.7, 666) then I can access all elements as in mylist[[2]] without a name but mylist$pi lets me access that item by name and mylist[["e"]] and I can also change the current values similarly. But without explaining what you want, my explanation likely is not what you need. But do note that even if you do not USE a package, you can sometimes use it indirectly by examining the code for a function you like. If it is primarily written in R, you may see how it does something and take a part of the code and use it yourself. -Original Message- From: R-help On Behalf Of Anupam Tyagi Sent: Tuesday, July 11, 2023 11:49 PM To: r-help mailing list Subject: [R] Variable and value labels Hello, is there an easy way to do variable and value labels (for factor variables) in base-R, without using a package. If not, what is an easy and good way to do labels, using an add-on package. -- Anupam. [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Variable and value labels
Very interesting! Thanks! On Wed, 12 Jul, 2023, 7:58 pm Viechtbauer, Wolfgang (NP), < wolfgang.viechtba...@maastrichtuniversity.nl> wrote: > For value labels, factor() provides the necessary machinery. For variable > labels, one could make use of comment() (something that not too many people > seem to know about). To illustrate: > > # look at the mtcars dataset > mtcars > > # turn 'am' into a factor and use labels for the two possible levels > thereof > mtcars$am <- factor(mtcars$am, levels=c(0,1), > labels=c("automatic","manual")) > mtcars > > # add a variable label via comment() > comment(mtcars$am) <- "Type of transmission" > > # extract all the variable labels > sapply(mtcars, comment) > > Best, > Wolfgang > > >-Original Message- > >From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Anupam > Tyagi > >Sent: Wednesday, 12 July, 2023 14:11 > >To: Martin Maechler > >Cc: r-help mailing list > >Subject: Re: [R] Variable and value labels > > > >Thanks. Sorry, I have not consulted help pages, but did a web search. Web > >search results are referring to some package or the other for doing > labels. > >Thanks for letting me know that this can be done in base-R. I will try to > >find the relevant information in R documentation. > > > >On Wed, 12 Jul, 2023, 1:33 pm Martin Maechler, < > maech...@stat.math.ethz.ch> > >wrote: > > > >> > Anupam Tyagi > >> > on Wed, 12 Jul 2023 09:18:55 +0530 writes: > >> > >> > Hello, > >> > >> > is there an easy way to do variable and value labels (for > >> > factor variables) in base-R, without using a package. > >> > >> Yes, there are many. > >> How many help pages (in R , i.e. base-R) did you consult? > >> > >> Very general questions as the above are not very useful, > >> because typically the only concised answer can be yes/no. > >> > >> > >> > If not, what is an easy and good way to do labels, using an > >> > add-on package. > >> > >> > >> > -- > >> > Anupam. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Variable and value labels
For value labels, factor() provides the necessary machinery. For variable labels, one could make use of comment() (something that not too many people seem to know about). To illustrate: # look at the mtcars dataset mtcars # turn 'am' into a factor and use labels for the two possible levels thereof mtcars$am <- factor(mtcars$am, levels=c(0,1), labels=c("automatic","manual")) mtcars # add a variable label via comment() comment(mtcars$am) <- "Type of transmission" # extract all the variable labels sapply(mtcars, comment) Best, Wolfgang >-Original Message- >From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Anupam Tyagi >Sent: Wednesday, 12 July, 2023 14:11 >To: Martin Maechler >Cc: r-help mailing list >Subject: Re: [R] Variable and value labels > >Thanks. Sorry, I have not consulted help pages, but did a web search. Web >search results are referring to some package or the other for doing labels. >Thanks for letting me know that this can be done in base-R. I will try to >find the relevant information in R documentation. > >On Wed, 12 Jul, 2023, 1:33 pm Martin Maechler, >wrote: > >> > Anupam Tyagi >> > on Wed, 12 Jul 2023 09:18:55 +0530 writes: >> >> > Hello, >> >> > is there an easy way to do variable and value labels (for >> > factor variables) in base-R, without using a package. >> >> Yes, there are many. >> How many help pages (in R , i.e. base-R) did you consult? >> >> Very general questions as the above are not very useful, >> because typically the only concised answer can be yes/no. >> >> >> > If not, what is an easy and good way to do labels, using an >> > add-on package. >> >> >> > -- >> > Anupam. __ R-help@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.
Re: [R] Variable and value labels
Thanks. Sorry, I have not consulted help pages, but did a web search. Web search results are referring to some package or the other for doing labels. Thanks for letting me know that this can be done in base-R. I will try to find the relevant information in R documentation. On Wed, 12 Jul, 2023, 1:33 pm Martin Maechler, wrote: > > Anupam Tyagi > > on Wed, 12 Jul 2023 09:18:55 +0530 writes: > > > Hello, > > > is there an easy way to do variable and value labels (for > > factor variables) in base-R, without using a package. > > Yes, there are many. > How many help pages (in R , i.e. base-R) did you consult? > > Very general questions as the above are not very useful, > because typically the only concised answer can be yes/no. > > > > If not, what is an easy and good way to do labels, using an > > add-on package. > > > > -- > > Anupam. > > > [[alternative HTML version deleted]] > > > __ > > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Variable and value labels
> Anupam Tyagi > on Wed, 12 Jul 2023 09:18:55 +0530 writes: > Hello, > is there an easy way to do variable and value labels (for > factor variables) in base-R, without using a package. Yes, there are many. How many help pages (in R , i.e. base-R) did you consult? Very general questions as the above are not very useful, because typically the only concised answer can be yes/no. > If not, what is an easy and good way to do labels, using an > add-on package. > -- > Anupam. > [[alternative HTML version deleted]] > __ > R-help@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@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.