I am triying to obtain a histogram. This is my code:
ggplot(data, aes(x=skus, fill=as.factor(stars))) +
+ geom_histogram(binwidth=.5, alpha=.5, position="identity") +
+ geom_vline(data=cdf, aes(xintercept=rating.mean, colour=as.factor(stars)),
+ linetype="dashed", size=1)
When I execute this code I obtain the next graphic:
This is not a histogram. What is my code mistake?
Thanks!
Just I have detected my mistake. I am defined the binwidth as .5. I only have to increment this rate to obtain a good histogram like binwidth=50
Related
I want half my chart background to be yellow. I've been thinking that I could definitely use a rectangle to put it under the graph.
ggplot(df2, aes(x=zeitpunkt.faktor, y=weight, group=group, color=group)) +
geom_errorbar(aes(ymin=weight-sd, ymax=weight+sd), width=.2) +
geom_line() +
geom_point() +
labs(x="Zeit in Wochen", y = "Gewicht in g") +
scale_color_brewer(palette="Paired")+
theme_minimal() +
labs(colour="Gruppe") +
geom_rect(df2, mapping=aes(xmin=0.5, xmax=7.5, ymin=-Inf, ymax=+Inf), fill="yellow", alpha=0.01, inherit.aes = FALSE)
However, I had the problem that my rectangle lies over the graph and hides it (see picture 1).
I searched something (and found this here: Refining ggplot R code so annotated rectangle appears behind plot points), changed the order and then got the following error "Discrete value supplied to continuous scale", so I didn't enter my x-variable as factor anymore.
ggplot(df3, aes(x=zeitpunkt, y=weight, group=group, color=group)) +
geom_rect(df3, mapping=aes(xmin=-0.5, xmax=13, ymin=-Inf, ymax=+Inf), fill="yellow", alpha=0.01, inherit.aes = FALSE) +
geom_line() +
geom_point() +
geom_errorbar(aes(ymin=weight-sd, ymax=weight+sd), width=.2) +
labs(x="Zeit in Wochen", y = "Gewicht in g") +
scale_color_brewer(palette="Paired")+theme_minimal() +
labs(colour="Gruppe")
Now the rectangle fits, but I only have some numbers on the x-axis and not all, like when using the factor (see picture 2).
My question is: How can I get my rectangle behind the graph and at the same time my x-axis like in the first picture?
Thanks for your help (and sorry for the question, I'm really still a beginner at R)!
I want to plot a negative binomial distribution and a Poisson distribution to fit my real data, but I don't know how to plot a legend, who can help me with that, thanks a lot. My code and picture is as follows:
ggplot() +
geom_density(aes(a),color="red",lwd=2) +
geom_density(aes(x=rpois(50,1.57)),color="purple",lwd=2) +
geom_smooth() +
geom_density(aes(x=rnbinom(100,size=0.2,mu=1.57)),color="blue",lwd=2) +
geom_smooth() +
coord_cartesian(xlim=c(0,10)) + labs(x="count")
And my data was uploaded here:
https://www.jianguoyun.com/p/DSHXKgMQm5CLBhiKjCc.
The easiest way to add a legend is to map a variable to color. For example
ggplot() +
geom_density(aes(a, color="data"),lwd=2) +
geom_density(aes(x=rpois(50,1.57), color="poisson"),,lwd=2) +
geom_smooth() +
geom_density(aes(x=rnbinom(100,size=0.2,mu=1.57),color="binomial"),lwd=2) +
geom_smooth() +
coord_cartesian(xlim=c(0,10)) + labs(x="count")
If I plot this
dodge <- position_dodge(.35)
ggplot(mediat, aes(x=t, y=Value, colour=factor(act),group=id )) +
geom_point(position=dodge) + geom_errorbar(aes(ymin=Value-sdt, ymax=Value+sdt),
width=0, position=dodge) + theme_bw() + geom_smooth(method="lm",se=FALSE,
fullrange=TRUE)
I get this
As you can see the regression line is not plotted.
with +stat_smooth(method=lm, fullrange=TRUE, se = FALSE) the result is the same.
I've found that removing the "group=id" I can get the regression lines but
then
ggplot(mediat, aes(x=t, y=Value, colour=factor(act) ))+ geom_point(position=dodge) +
geom_errorbar(aes(ymin=Value-sdt, ymax=Value+sdt), width=0, position=dodge) +
theme_bw() + geom_smooth(method="lm",se=FALSE, fullrange=TRUE)
As you can see, now it plot the lines but I loose the dodge function by groups.
How can I get both things at once?. I mean, regression lines by "id" on the first uncluttered plot?
Any other solution with base plot, lattice or any other common package would also be welcome.
Regards
I am trying to plot the outliers and mean point for the box plots in below using the data available here. The dataset has 3 different factors and 1 value column for 3600 rows.
While I run the below the code it shows the mean point but doesn't draw the outliers properly
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
Again, while I am modify the code like in below the mean points disappear !!
ggplot(df, aes(x=Representations, y=Values, colour=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
In both of the cases I am getting the message: "ymax not defined: adjusting position using y instead" 3 times.
Any kind suggestions how to fix it? I would like to draw the mean points within individual box plots and show outliers in the same colour as the plots.
EDIT:
The original data set does not have any outliers and that was reason for my confusion. Thanks to MrFlick's answer with randomly generated data which clarifies it properly.
Rather than downloading the data, I just made a random sample.
set.seed(18)
gg <- expand.grid (
Methods=c("BC","FD","FDFND","NC"),
Metrics=c("DM","DTI","LB"),
Representations=c("CHG","QR","HQR")
)
df <- data.frame(
gg,
Values=rnorm(nrow(gg)*50)
)
Then you should be able to create the plot you want with
library(ggplot2)
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
stat_summary(fun.y="mean", geom="point",
position=position_dodge(width=0.75), color="white") +
facet_wrap(~Metrics)
which gave me
I was using ggplot2 version 0.9.3.1
I am plotting some data series along with regression lines using this code:
ggplot(dt1.melt, aes(x=lower, y=value, group=variable, colour=variable)) +
geom_point(shape=1) +
geom_smooth(method=lm,
se=FALSE)
However, I need to constrain the regression line to be through the origin for all series - in the same way as abline(lm(Q75~-1+lower,data=dt1)) would achieve on a standard R plot.
Can anyone explain how to do this in ggplot ?
You need to specify this in the formula argument to geom_smooth:
... + geom_smooth(method=lm, se=FALSE, formula=y~x-1)