I am trying to produce a ggplot that shows the histogram of the data as well as two density curves in which one has no adjust value and the other one has.
I tried the following code:
ggplot(df, aes_string(x=value))+
geom_histogram(aes(y=..density..), colour="grey", fill="grey", alpha=.3)+
geom_density(colour="red", fill="red", alpha=.3)+
stat_density(bw="SJ", alpha=0)+
geom_density(colour="blue", fill="blue", alpha=.3)+
stat_density(bw="SJ", adjust=5, alpha=0)+
theme_bw()
But this produces this graph with both curves overlapping 100%...
The .txt dataframe used is on my google drive
Thanks in advance!
Does adding a specific adjust argument to geom_density not do what you want?
ggplot(df, aes(x=value))+
geom_histogram(aes(y=..density..), colour="grey", fill="grey", alpha=.3)+
geom_density(colour="red", fill="red", alpha=.3, adjust = 1)+
geom_density(colour="blue", fill="blue", alpha=.3, adjust = 2)+
theme_bw()
Related
I have a grid of plots, all with the same y and x-axis scale. The plots represent time in the x-axe and mean values in the y-axe with their standard errors. My problem is that some errorbars are not entirely within the plot margins, and I wonder if there is some way to represent the part of the errorlines that are within the plot margins. Below I give a fake example and code to play with:
df <- data.frame(time=seq(-15,15,1),
mean=c(0.49,0.5,0.53,0.55,0.57,0.59,0.61,0.63,0.65,0.67,0.69,0.71,0.73,0.75,0.77,0.79,0.77,0.75,0.73,0.71,0.69,0.67,0.65,0.63,0.61,0.59,0.57,0.55,0.53,0.51,0.49),
sd=c(0.09,0.087,0.082,0.08,0.023,0.011,0.010,0.009,0.008,0.007,0.006,0.005,0.004,0.003,0.002,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.010,0.011,0.023,0.08,0.084,0.087,0.09))
Plot <- ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(limits = c(0.49, 0.85), breaks = c(0.5, 0.65,0.8))
Plot
You need to set coord_cartesian limits rather than scale_y_continuous limits:
ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(breaks = c(0.5, 0.65,0.8)) +
coord_cartesian(ylim = c(0.49, 0.85))
I would like to include a legend inside the top right of my plot, indicating the parameter values of the plots.
ggplot() +
geom_line(data=data1, aes(x=x, y=y1), color='green', linetype = "twodash", size=0.5) +
geom_line(data=data2, aes(x=x, y=y2), color='red', linetype="longdash", size=0.5) +
geom_line(data=data3, aes(x=x, y=y3), color='blue') +
theme_classic() +
labs(x='input, x',
y='output, f(x)')
Can someone please say how this is done. Thanks.
I think this is a very good source of "what to do with ggplot"
http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#changing-the-position-of-the-legend
from there
# Position legend in graph, where x,y is 0,0 (bottom left) to 1,1 (top right)
bp + theme(legend.position=c(1, 1))
should/could do it?
I have the following code:
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
stat_summary(fun.y=mean, geom='point', size=2, fill='white')
I know there are probably other ways of plotting this mean using the iris data. For my own data, though, it is the only way.
PROBLEM: the code above doesn't give white-filled points, but solid black points. Is there ar way to set the fill-colour when using the stat_summary argument?
Thanks!
Either use color instead of fill
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
stat_summary(fun.y=mean, geom='point', size=2, color='white')
or use a symbol shape that has a fill and a border color
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
stat_summary(fun.y=mean, geom='point', size=2, shape=21, fill="blue", color="red")
How can I make the lines thicker without adding geom_line
ggplot(df, aes(x=c, y=d, colour=group)) +
geom_point()+
geom_smooth( method=lm,se=FALSE, fullrange=TRUE)
Thank you
When using a discrete values ggplot2 provides a gridline at the tick value at the centre of the value
library(reshape2)
ggplot(data=tips, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge())
How can I set the grid line from the x axis to appear between the discrete values (i.e. between 'Dinner' and 'Lunch')
I have tried to set panel.grid.minor.x however (I think) as it is discrete this does not work ... this is not a minor value for it to plot the girdline on.
ggplot(data=tips, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge()) +
theme(panel.grid.minor.x = element_line())
You can add a vertical line that will act as a grid line as follows:
geom_vline(xintercept=1.5, colour='white')
You can, of course, alter line width, colour, style, etc. as needed, and add multiple lines in the appropriate locations if you have several groups of bars that need to be separated by grid lines. For example, using some fake data:
set.seed(1)
dat = data.frame(total_bill=rnorm(100,80,10),
sex=rep(c("Male","Female"),50),
time=sample(c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"),
100, replace=TRUE))
dat$time = factor(dat$time,
levels=c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"),
ordered=TRUE)
ggplot(data=dat, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge()) +
geom_vline(xintercept=seq(1.5, length(unique(dat$time))-0.5, 1),
lwd=1, colour="black")
Had the same problem. My solution was to make the grid line bigger ..
set.seed(1)
dat = data.frame(total_bill=rnorm(100,80,10),
sex=rep(c("Male","Female"),50),
time=sample(c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"),
100, replace=TRUE))
dat$time = factor(dat$time,
levels=c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"),
ordered=TRUE)
ggplot(data=dat, aes(x=time, y=total_bill, color=sex)) +
geom_point(stat="identity", position=position_dodge()) +
theme(panel.grid.major.x = element_line(color = "white", size = 20))