"ColSideColors" in heatmap made bye ggplot2? - r

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.

Related

How do I plot a scatterplot graph with a repeated-measures variable and a continuous variable in r?

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!

Change colors in r plot

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")

Making one variable be shapes of different colors (ggplot2)

So right now I've got this plot:
my plot
(sorry it's not inline image, this is my first time on Stack Overflow and it wouldn't let me post images)
The plot is produced with this code:
ggplot(potassium.data,
aes(x=Experiment,y=value,
colour=Pedigree))+geom_jitter()+labs(title=element)
The problem is, there are 31 different maize pedigrees being plotted here, so it's difficult to distinguish the colors from each other. I was wondering if it's possible to make it so that the color and shape of the point are used to uniquely identify a pedigree, so that for example one pedigree is red squares, another is red circles, a third one is blue squares, a fourth is blue circles, and so on. This would make it far easier to distinguish the points. Anyone know how to do this?
I don't think thats possible, if you do the shaping by pedigree you will just end up with as many categories of shapes as you have colors now.
geom_label() and geom_text() would let you plot the cultivar id directly onto the plot, then maybe you could build a separate column for something equivalent to genus, so that the cultivars could be grouped somehow (maybe A, B, PH, etc). Then you could color by that "genus" column, which would make the plot look better:
ggplot(potassium.data,
aes(x=Experiment,y=value, label=Pedigree, colour = genus))+
geom_label(position = position_jitter())+
labs(title=element)
Ideally you would end up with a plot colored by the genus while only plotting the suffix digits currently in Pedigree.
I have to agree with Nathan and Joran, the plot is quite confusing by having so many different points and adding shapes into the mix is unlikely to help.
To answer your question you should be able to use shape=pedigree, but maybe to make the graph more readable you could join the pedigrees from one experiment to the other with a geom_line so the reader spends less time scanning.

R: How to automatically set the color of different groups in survival plot

I am plotting the survival probability for my dataframe with 8 different groups with this command:
fit2<-Surv((time=t2$uptimeDay,event=t2$solved,type='right')~t2$cluster)
plot(fit2,conf.int=F,xlim=c(0, 250),mark.time=c(1,50,100,200),mark=c(1,3,4,2,5,7,6,8,9,10),lwd=1,cex=0.7,lty = 1:11,xlab='Time(days)',ylab='Survival Probability')
the cluster here is a number between 1 and 10.
I would like to know how to automatically set the colors of the curves together with an automatic legend using key of the curves.
Can somebody help me out with this?
I have a function that I use for Kaplan-Meier curves that is based on ggplot2, which will take care of the colors and legends for you. Regrettably, I've not gotten around to packaging it up in any sensible way. But you can download the source code from
https://gist.github.com/nutterb/004ade595ec6932a0c29
And some examples on how to use it from
https://gist.github.com/nutterb/fb19644cc18c4e64d12a
It's not clear what you mean by making this "automatic" and the desire to "use the key of the curves", but perhaps you are asking that the colors of the curves match the legend.
png()
mycols=c("red","blue")
plot(prio.fit, fill=mycols)
legend(x="bottomleft", col=mycols, legend=mycols)
dev.off()
If you want this mated to a dataset and wanted to specify particular colors for your groups, then you will need to provide a dataset so there is something meaningful to use as labels, and be more specific about the coloring schema needed.

Plotting a Bar graph

I have a dataset as follows:
(10,75)
(20,80)
(50,85)
(100,92)
How to plot a bar-graph in R? I saw many examples in the net but none of them conform to this simple circumstance. Thanks
Try this:
data1=rbind(c(10,20,50,100),c(75,80,85,92))
barplot(data1, beside=TRUE, col=c("blue", "red"))
As an alternative, you can always use the ggplot2 library. Because of the way the data is shaped, you should also use the reshape2 library to differentiate between variables. It's a bit more complicated in this case, but in general you'll get nicer-looking barplots.
library(ggplot2)
library(reshape2)
#id variable tells what row number is used
data1=as.data.frame(cbind(id=1:4,var1=c(10,20,50,100),var2=c(75,80,85,92)))
#melt will create a row for each variable of each row, except it saves the id as a separate variable that's on every row
data1=melt(data1,id.vars='id')
#ggplot tells what data set is used and which variables do what
#geom_bar tells what type of plot should be used and certain options for the plot
ggplot(data1,aes(x=id,y=value,fill=variable))+geom_bar(stat='identity',position='dodge')

Resources