This is the basic graph R presents when plotting a data frame.
plot(df)
It displays the relationship between all variables.
I know about faceting in ggplot2 but it's used for partition according to specific variables. I want to facet by a target parameter (for color) and split the grid by the variables.
sample data:
prediction.date mean.forcast mean.Error standard.Deviation AIC param.u param.v
2012-08-29 0.0015608102 0.008296402 0.008296402 -6.165365 2 5
2012-08-30 -0.0002720289 0.008537309 0.008537309 -6.164167 2 4
2012-09-02 -0.0014277972 0.008194409 0.008194409 -6.168868 4 0
2012-09-03 0.0016537998 0.008062687 0.008062687 -6.176634 5 3
2012-09-04 -0.0030247699 0.007885009 0.007885009 -6.181844 4 3
2012-09-05 0.0001538991 0.007524703 0.007524703 -6.197240 3 4
If you just need to color points in plot you provided, then you can use argument col= in plot() and set names of colors and variable to use in determining color.
#variable of test result (should be the same length as number of rows in df)
test.result<-c(0,1,1,0,0,1)
plot(df[,3:7],col=c("green","red")[as.factor(test.result)])
Related
I am plotting data series with gnuplot with command:
p 'file.txt' u 1:2:3 with labels
and got the graph with a lot of labels as below
which looks messy. So, i use different command:
p 'file.txt' u 1:2:3 with points pt 5 palette
which showed beautiful graph with colour spectrum.
But it did not show the labels. Acutally I don't need to show all labels, but I would like to show lowest five and highest five values.
How can I mix these two commands so that I can show the graph with colour spectrum with 10 labels (5 for lowest five and another 5 for highest five). Thanks.
The labels style accepts a tc palette option
Thus you can do
plot datafile u 1:2:3:3 with labels tc palette
For example, with the following data
1 1 30
1 2 40
2 2 30
2 1 35
3 3 10
3 4 15
using plot datafile u 1:2:3:3 with labels tc palette will plot
In order to filter to only the top 5 and bottom 5 numbers, you will need to do some pre-processing of your data outside of gnuplot.
I have the following dataframe and I am using ggplot to plot the ind vs values.
ggplot(data=stats,aes(x=ind,y=values,fill=ind))+geom_bar(stat="identity")+coord_flip()+scale_fill_brewer()
stats
values ind
1 238970950 testdb_i
2 130251496 testdb_b
3 314350612 testdb_s
4 234212341 testdb_m
5 222281421 testdb_e
6 183681071 testdb_if
7 491868567 testdb_l
8 372612463 testdb_p
The plot in y-axis is in the form of 0e+00, 1e+08, 2e+08 and so on but instead I need it in the form of 100M(hundred million), 200M(two hunderd million) etc marks. How can I get the desired axes in ggplot?
You may try
ggplot(data=stats,aes(x=ind,y=values,fill=ind))+
geom_bar(stat="identity")+
coord_flip()+
scale_fill_brewer()+
scale_y_continuous(labels=function(x) paste0(x/1e6,"M"))
Could anyone help me to plot the data below as a density plot where colour=variable?
> head(combined_length.m)
length seq mir variable value
1 22 TGAGGTATTAGGTTGTATGGTT mmu-let-7c-5p Ago1 8.622468
2 23 TGAGGGAGTAGGTTGTATGGTTT mmu-let-7c-5p Ago1 22.212471
3 21 TGAGGTAGTAGGTTGCATGGT mmu-let-7c-5p Ago1 9.745199
4 22 TGAGGTAGTATGTTGTATGGTT mmu-let-7c-5p Ago1 11.635982
5 22 TGAGTTAGTAGGTTGTATGGTT mmu-let-7c-5p Ago1 13.203627
6 20 TGAGGTAGTAGGCTGTATGG mmu-let-7c-5p Ago1 7.752571
ggplot(combined_length.m, aes(factor(length),value)) + geom_bar(stat="identity") + facet_grid(~variable) +
theme_bw(base_size=16
I tried this without success:
ggplot(combined_length.m, aes(factor(length),value)) + geom_density(aes(fill=variable), size=2)
Error in data.frame(counts = c(167, 9324, 177, 150451, 62640, 74557, 4, :
arguments imply differing number of rows: 212, 6, 1, 4
I want something like this:
http://i.stack.imgur.com/qitOs.jpg
Using factor(length) for x seems to create problems. Just use length.
Also, density plots display the distribution of whatever you define as x. So by definition the y axis is the density at a given value of x. In your code you seem to be trying to specify both x and y, which makes no sense. You can specify a y in geom_density(...) but this controls the scaling, as shown below. [Note: Your example has only one type of variable (Ago1) so I created an artificial dataset].
set.seed(1) # for reproducible example
df <- data.frame(variable=rep(LETTERS[1:3],c(5,10,15)),
length =rpois(30,25),
value =rnorm(30,mean=20,sd=5))
library(ggplot2)
ggplot(df,aes(x=length))+geom_density(aes(color=variable))
In this representation, the area under each curve is 1. This is the same as setting y=..density..
ggplot(df,aes(x=length))+geom_density(aes(color=variable,y=..density..))
You can also set y=..count.. which scales based on the counts. In this example, since there are 15 observations for C and only 5 for A, the blue curve (C) has three times the area as the red curve (A).
ggplot(df,aes(x=length))+geom_density(aes(color=variable,y=..count..))
You can also set y=..scaled.. which adjusts the curves so the maximum value in each corresponds to 1.
ggplot(df,aes(x=length))+geom_density(aes(color=variable,y=..scaled..))
Finally, if you want to get rid of all those annoying extra lines, use stat_density(...) instead:
ggplot(df,aes(x=length))+
stat_density(aes(color=variable),geom="line",position="identity")
I want to plot 2 graphs in 1 frame. Basically I want to compare the results.
Anyways, the code I tried is:
plot(male,pch=16,col="red")
lines(male,pch=16,col="red")
par(new=TRUE)
plot(female,pch=16,col="green")
lines(female,pch=16,col="green")
When I run it, I DO get 2 plots in a frame BUT it changes my y-axis. Added my plot below. Anyways, y-axis values are -4,-4,-3,-3,...
It's like both of the plots display their own axis.
Please help.
Thanks
You don't need the second plot. Just use
> plot(male,pch=16,col="red")
> lines(male, pch=16, col = "red")
> lines(female, pch=16, col = "green")
> points(female, pch=16, col = "green")
Note: that will set the frame boundaries based on the first data set, so some data from the second plot could be outside the boundaries of the plot. You can fix it by e.g. setting the limits of the first plot yourself.
For this kind of plot I usually like the plotting with ggplot2 much better. The main reason: It generalizes nicely to more than two lines without a lot of code.
The drawback for your sample data is that it is not available as a data.frame, which is required for ggplot2. Furthermore, in every case you need a x-variable to plot against. Thus, first let us create a data.frame out of your data.
dat <- data.frame(index=rep(1:10, 2), vals=c(male, female), group=rep(c('male', 'female'), each=10))
Which leaves us with
> dat
index vals group
1 1 -0.4334269341 male
2 2 0.8829902521 male
3 3 -0.6052638138 male
4 4 0.2270191965 male
5 5 3.5123679143 male
6 6 0.0615821014 male
7 7 3.6280155376 male
8 8 2.3508890457 male
9 9 2.9824432680 male
10 10 1.1938052833 male
11 1 1.3151289227 female
12 2 1.9956491556 female
13 3 0.8229389822 female
14 4 1.2062726250 female
15 5 0.6633392820 female
16 6 1.1331669670 female
17 7 -0.9002109636 female
18 8 3.2137052284 female
19 9 0.3113656610 female
20 10 1.4664434215 female
Note that my command assumes you have 10 data values each. That command would have to be adjusted according to your actual data.
Now we may use the mighty power of ggplot2:
library(ggplot2)
ggplot(dat, aes(x=index, y=vals, color=group)) + geom_point() + geom_line()
The call above has three elements: ggplot initializes the plot, tells R to use dat as datasource and defines the plot aesthetics, or better: Which aesthetic properties of the plot (such as color, position, size, etc.) are influenced by your data. We use the x and y-values as expected and furthermore set the color aesthetic to the grouping variable - that makes ggplot automatically plot two groups with different colors. Finally, we add two geometries, that pretty much do what is written above: Draw lines and draw points.
The result:
If you have your data saved in the standard way in R (in a data.frame), you end with one line of code. And if after some thousands years of evolution you want to add another gender, it is still one line of code.
Hi dear I have a little problem with a graphic in ggplot, I want to design a graphic that shows in x axis a varible that is a factor and in y axis the values of two continuous variables, to see the difference between first continuous and second variable related to the factor variable. The data frame is similar to this:
Group Var1 Var2
1 10 20
2 15 30
3 5 10
4 20 15
5 5 5
My objective is to see the difference between var1 and var to in each member of factor. It is possible to make this in ggplot. Thanks a lot of.
Usualy, you should reshape your data in the long format to compare between variable. For example using melt from reshape2
library(reshape2)
dat.m <- melt(dat,id.vars='Group')
Then , for example, I am plotting here a geom_bar to compare between levels. Of course you can choose another geom.
library(ggplot2)
ggplot(dat.m)+
geom_bar(aes(x=Group,y=value,fill=variable),
stat='identity',position='dodge')