I have a four levels repeated measures variable (let's call it RM) and I have a continuous variable predictor (let's call it C).
I want to plot a scatterplot graph with C on the X-Axis and RM on the Y-Axis with different lines within the plot for each level of RM.
Is this possible to do with ggplot or a similar package?
Thanks in advance!
Utilizing ggplot2, you should be able to achieve this type of graphical output. Viewing a portion of your data that you wish to plot would be beneficial to provide a sufficient answer.
I have attached a link to a summary of ggplot2 graphical functions here. This link provides some background on ggplot2 and the components necessary to create a graph. Three components are needed in ggplot2 to make a graph, the data, the ggplot2 function, and mapping your variables with the aesthetic.
Because you don't have a representation of some of your data, providing a sufficient answer is difficult, but it might look something like this.
ggplot(data=yourdata, aes(x= C(continuous_variable, y = RM(repeated_measures)) +
geom_point()
You may also map geom_line for each RM variable in addition to this example. Hope this helps!
I am currently trying to plot some data and don't manage to obtain a nice result. I have a set of 51 individuals with each a specific value (Pn) and split within 14 groups. The closest thing I end up with is this kind of plot. I obtain it thanks to the simple code bellow, starting by ordering my values for the Individuals :
Individuals <- factor(Individuals,levels=Individuals[order(Pn)])
dotchart(Pn,label=Individuals,color=Groups)
The issue is that I only have 9 colors on this plot (so I lost information somehow) and I can't manage to find a way to apply manually one color per group.
I've also try to use the ggplot2 package by reading it could give nice looking things. In that case I can't manage to order properly the Individuals (the previous sorting doesn't seem to have any effect here), plus I end up with only different type of blue for the group representation which is not an efficient way to represent the information given by my data set. The plot I get is accessible here and I used the following code:
ggplot(data=gps)+geom_point(mapping=aes(x=Individuals, y=Pn, color=Groups))
I apologize if this question seems redundant but I couldn't figure a solution on my own, even following some answer given to others...
Thank you in advance!
EDIT: Using the RColorBrewer as suggested bellow sorted out the issue with the colors when I use the ggplot2 package.
I believe you are looking for the scale_color_manual() function within ggplot2. You didn't provide a reproducible example, but try something along the lines of this:
ggplot(data=gps, mapping=aes(x=Individuals, y=Pn, color=Groups))+
geom_point() +
scale_color_manual(values = c('GROUP1' = 'color_value_1',
'GROUP2' = 'color_value_2',
'GROUP3' = 'color_value_3'))
Replace GROUPX with the values inside your Group column, and replace color_value_x with whatever colors you want to use.
A good resource for further learning about ggplot2 is chapter 3 of R For Data Science, which you can read here: http://r4ds.had.co.nz/data-visualisation.html
I can't be sure without looking at your data, but it looks like Groups may be a numeric value. Try this:
gps$Groups <- as.factor(gps$Groups)
library(RColorBrewer)
ggplot(data=gps)+
geom_point(mapping=aes(x=Individuals, y=Pn, color=Groups))+
scale_colour_brewer(palette = "Set1")
I have plotted my data in both base R and ggplot methods to see how the plots look different, and my graph from ggplot() form looks wrong. It should look like it does when I graph it in base R. Shown below is my base R code and my ggplot code, and the graphs that each produce.
Base R code:
em is though.
Use geom_path() instead of geom_line() to preserve the ordering of the dataset. This is documented in ?geom_line
There is a very useful option "ColSideColors" in the native "heatmap" function in R, but how to implement this effect by ggplot2?
As shown in the heatmap, I would like to make the red and blue bar to represent different groups with ggplot2.
Thanks in advance.
I would say what you are looking for is facetting - in your case you would want to have a factor that distinguishes data entries between the red and the blue part and then have a facet_grid() to split up the graph with respective labels. Say your factor is called subset in the melted dataframe, you would need to add the following to your plot:
facet_grid(. ~ subset)
For further details, have a look at the facet_grid() documentation.
And for an answer that more explicitly addresses your problem, you should describe your problem in more detail. Have a look at some info on how to produce a great reproducible example in R.
This is a repeat of a question originally asked here: Indicating the statistically significant difference in bar graph but asked for R instead of python.
My question is very simple. I want to produce barplots in R, using ggplot2 if possible, with an indication of significant difference between the different bars, e.g. produce something like this. I have had a search around but can't find another question asking exactly the same thing.
I know that this is an old question and the answer by Didzis Elferts already provides one solution for the problem. But I recently created a ggplot-extension that simplifies the whole process of adding significance bars: ggsignif
Instead of tediously adding the geom_path and annotate to your plot you just add a single layer geom_signif:
library(ggplot2)
library(ggsignif)
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
geom_signif(comparisons = list(c("versicolor", "virginica")),
map_signif_level=TRUE)
Full documentation of the package is available at CRAN.
You can use geom_path() and annotate() to get similar result. For this example you have to determine suitable position yourself. In geom_path() four numbers are provided to get those small ticks for connecting lines.
df<-data.frame(group=c("A","B","C","D"),numb=c(12,24,36,48))
g<-ggplot(df,aes(group,numb))+geom_bar(stat="identity")
g+geom_path(x=c(1,1,2,2),y=c(25,26,26,25))+
geom_path(x=c(2,2,3,3),y=c(37,38,38,37))+
geom_path(x=c(3,3,4,4),y=c(49,50,50,49))+
annotate("text",x=1.5,y=27,label="p=0.012")+
annotate("text",x=2.5,y=39,label="p<0.0001")+
annotate("text",x=3.5,y=51,label="p<0.0001")
I used the suggested method from above, but I found the annotate function easier for making lines than the geom_path function. Just use "segment" instead of "text". You have to break things up by segment and define starting and ending x and y values for each line segment.
example for making 3 lines segments:
annotate("segment", x=c(1,1,2),xend=c(1,2,2), y= c(125,130,130), yend=c(130,130,125))