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')
Related
Okay so I have an assignment where I need to conduct a graph that best represents the before and after affects of two streams. The graph(s) have to contain means and standard error for each stream in each year.. I cannot figure the proper coding for the graph. I continue to get errors and bad graphs. I will attach a sample of what the data looks like too.
A sample of the data, it changes to after at 51
Try to post a reproducible error or specification of your problem.
As far as I can analyze your problem, you maybe should not create b4, because it does not seem to be an effective subset. If you want to assemble certain plots, you can use plot_grid from cowplot.
Otherwise you can add facet_wrap(~ VARIABLE_NAME) to ggplot in order to create many plots divided by deviating observations in the specified variable.
If you are not happy with the visual outcome and result of your graph, you can choose another theme, e.g. theme_bw() which can be simply added to your ggplot function. You can add and change further labels with labs() and theme().
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 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.
I have the following plot:
plot.ts(returns)
I have another dataframe ma_sd which contains the rolling SD from moving averages of the above returns. The df is structured exactly like returns. Is there a simple way to add each line to the corresponding plots?
lines(1:N, ma_sd) seemed intuitive, but it does not work.
Thanks
The only way I can see you doing this is to plot them separately. This code is a bit clunky but will allow you full flexibility to be able to specify labels and axis ranges. You can build on this.
par(mfrow=c(3,1),oma=c(5,4,4,2),mar=c(0,0,0,0))
time<-as.data.frame(matrix(c(1:length(returns[,1])),length(returns[,1]),3))
plot(time[,1],returns[,1],type='l',xaxt='n')
points(time[,1],ma_sd[,1],type='l',col='red')
plot(time[,2],returns[,2],type='l',xaxt='n')
points(time[,2],ma_sd[,2],type='l',col='red')
plot(time[,3],returns[,3],type='l')
points(time[,3],ma_sd[,3],type='l',col='red')
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.