I have six plots obtained with ggplot2 for normality analysis: 2 histograms, 2 qqplots and 2 boxplots.
I want to display them together ordered by type of plot: so the histograms in the first row, the qqplots in the second row and the boxplots in the third row. For this I use the grid.arrange function from gridExtra package as follows:
grid.arrange(grobs= list(plot1, plot2, qqplot1, qqplot2, boxplot1, boxplot2),
ncol=2, nrow=3,
top = ("Histograms + Quantile Graphics + Boxplots"))
But this error message pops up:
Error: stat_bin() requires an x or y aesthetic.
any idea how to solve this?
As people said in the comments the error was the aes() of one of the plots. The confussion came as R allows you to create an object even when it´s not operational, I guess this is because it can be modified later. This is the code for the plot:
ggplot(data = mtcars, aes(sample=mtcars$mpg)) +
geom_histogram(aes(y = ..density.., fill = ..count..), binwidth = 1) +
geom_density(alpha=.2) +
scale_fill_gradient(low = "#6ACE78", high = "#0D851D") +
stat_function(fun = dnorm, colour = "firebrick",
args = list(mean = mean(mtcars$mpg),
sd = sd(mtcars$mpg))) +
labs(x = "Tiempo de seguimiento", y = "")+
theme_bw()
As you can see, the mistake is the first aes() argument, as I wrote sample= instead of x=. Already solved.
Thanks
Related
I am having a difficult understanding why this code works and doesn't work. I want a plot by group + specify the number of columns of my legend. Basically, the only way I can get this to work is to specify both a fill and a colour variable in the aesthetic. It seems like fill allows me to change the columns and colour changes colors of the lines, but this feels a bit kludgy. Does anyone have a good understanding of the logic here, or a better way to accomplish this goal? I’ll accept a base plot answer!
My example code is below:
# ~ Library ~ #
require(ggplot2)
# Generate example data
x=1:10
y=10:1
data = data.frame(x_data=rep(1:10,2),
y_data=c(x,y),
group=c(rep('A',length(x)),rep('B',length(y))))
# ~ Plot data ~ #
# Only this works
plot = ggplot(data,aes(x=x_data,y=y_data,fill=group,colour=group)) + geom_line()
plot = plot + guides(fill = guide_legend(ncol = 2))
# This doesn't work
plot = ggplot(data,aes(x=x_data,y=y_data,fill=group)) + geom_line()
plot = plot + guides(fill = guide_legend(ncol = 2))
plot
# Neither does this
plot = ggplot(data,aes(x=x_data,y=y_data,colour=group)) + geom_line()
plot = plot + guides(fill = guide_legend(ncol = 2))
plot
As suggested by h-1, this works
#This works
plot = ggplot(data,aes(x=x_data,y=y_data,fill=group)) + geom_line()
plot = plot + guides(colour = guide_legend(ncol = 2))
plot
I'm using visual studio with R version 3.5.1 where I tried to plot legend to the graph.
f1 = function(x) {
return(x+1)}
x1 = seq(0, 1, by = 0.01)
data1 = data.frame(x1 = x1, f1 = f1(x1), F1 = cumtrapz(x1, f1(x1)) )
However, when I tried to plot it, it never give me a legend!
For example, I used the same code in this (Missing legend with ggplot2 and geom_line )
ggplot(data = data1, aes(x1)) +
geom_line(aes(y = f1), color = "1") +
geom_line(aes(y = F1), color = "2") +
scale_color_manual(values = c("red", "blue"))
I also looked into (How to add legend to ggplot manually? - R
) and many other websites in stackoverflo, and I have tried every single function in https://www.rstudio.com/wp-content/uploads/2016/11/ggplot2-cheatsheet-2.1.pdf
i.e.
theme(legend.position = "bottom")
scale_fill_discrete(...)
group
guides()
show.legend=TRUE
I even tried to use the original plot() and legend() function. Neither worked.
I thought there might be something wrong with the dataframe, but I split them(x2,f1,F1) apart, it still didn't work.
I thought there might be something wrong with IDE, but the code given by kohske acturally plotted legend!
d<-data.frame(x=1:5, y1=1:5, y2=2:6)
ggplot(d, aes(x)) +
geom_line(aes(y=y1, colour="1")) +
geom_line(aes(y=y2, colour="2")) +
scale_colour_manual(values=c("red", "blue"))
What's wrong with the code?
As far as I know, you only have X and Y variables in your aesthetics. Therefore there is no need for a legend. You have xlab and ylab to describe your two lines. If you want to have legends, you should put the grouping in the aesthetics, which might require recoding your dataset
d<- data.frame(x=c(1:5, 1:5), y=c(1:5, 2:6), colorGroup = c(rep("redGroup", 5),
rep("blueGroup", 5)))
ggplot(d, aes(x, y, color = colorGroup )) + geom_line()
This should give you two lines and a legend
I am trying to create a legend in ggplot2 for hlines and ablines using clues from other similar questions. I am close to getting what I need with the following code (and example image) but I can't seem to get rid of the extra lines crossing the legend icons.
p <- ggplot(mtcars, aes(x = wt, y=mpg, col = factor(cyl))) + geom_point()
p + geom_hline(aes(lty="foo",yintercept=20)) +
geom_hline(aes(lty="bar",yintercept=25)) +
geom_hline(aes(lty="bar",yintercept=30)) +
geom_abline(aes(lty = "regression", intercept = 10 , slope = 1)) +
scale_linetype_manual(name="",values=c(2,3,1))
This behavior in the legend only appears when I include the abline. Without it, both hline appear as intended in the legend.
What am I missing here?
As a secondary point: both hlines (labelled "bar" here) here use the exact same configuration, but have different values for yintercept. I wasn't able to draw both of them with the same command, receiving an error (Error: Aesthetics must be either length 1 or the same as the data (32): linetype, yintercept).
Whenever I copy&paste a command like this, it feels like I'm not doing it right. Is it possible to set two yintercepts, while manually defining the linetype to create a legend?
You can use argument show.legend in the geom_abline:
ggplot() +
geom_point(aes(x = mtcars$wt, y=mtcars$mpg, col = factor(mtcars$cyl))) +
geom_hline(aes(lty=c("foo", "bar","bar"),yintercept=c(20,25,30))) +
geom_abline(aes(lty = "regression", intercept = 10 , slope = 1), show.legend = F) +
scale_linetype_manual(name="",values=c(2,3,1) )
If you not define the data on the ggplot command you can define all the hlines in just one command:
ggplot(mtcars, aes(x = wt, y=mpg, col = factor(cyl))) + geom_point() +
geom_hline(aes(lty="foo",yintercept=20)) +
geom_hline(aes(lty="bar",yintercept=25)) +
geom_hline(aes(lty="bar",yintercept=30)) +
geom_abline(aes(lty = "regression", intercept = 10 , slope = 1), show.legend = F) +
scale_linetype_manual(name="",values=c(2,3,1) )
When I combine geom_vline() with facet_grid() like so:
DATA <- data.frame(x = 1:6,y = 1:6, f = rep(letters[1:2],3))
ggplot(DATA,aes(x = x,y = y)) +
geom_point() +
facet_grid(f~.) +
geom_vline(xintercept = 2:3,
colour =c("goldenrod3","dodgerblue3"))
I get an error message stating Error: Aesthetics must be either length 1 or the same as the data (4): colour because there are two lines in each facet and there are two facets. One way to get around this is to use rep(c("goldenrod3","dodgerblue3"),2), but this requires that every time I change the faceting variables, I also have to calculate the number of facets and replace the magic number (2) in the call to rep(), which makes re-using ggplot code so much less nimble.
Is there a way to get the number of facets directly from ggplot for use in this situation?
You could put the xintercept and colour info into a data.frame to pass to geom_vline and then use scale_color_identity.
ggplot(DATA, aes(x = x, y = y)) +
geom_point() +
facet_grid(f~.) +
geom_vline(data = data.frame(xintercept = 2:3,
colour = c("goldenrod3","dodgerblue3") ),
aes(xintercept = xintercept, color = colour) ) +
scale_color_identity()
This side-steps the issue of figuring out the number of facets, although that could be done by pulling out the number of unique values in the faceting variable with something like length(unique(DATA$f)).
I've been working on creating a bar graph with error bars to depict group differences for a dataset that I have. But the error bars are coming out funky, in that they are appearing further above the bar and in the middle of a bar.
My code:
ggplot(MRS_Hippo_NAA_Cre_Data_copy, aes(Type, Hippo_6_9NAACre, fill=Type)) +
geom_bar(stat="summary", fun.y="mean", colour="black", size=.3) +
geom_errorbar(aes(ymin=meanNAA-NAAse, ymax=meanNAA+NAAse), width=.2,
position=position_dodge(.9)) + labs(x="Group", y="Right Posterior NAA/Cre") +
scale_fill_manual(values=c("#0072B2", "#D55E00"), name="Group") + theme(text =
element_text(size=18))`
This produced this graph:
I calculated the standard error by using the following function:
std <- function(x) sd(x)/sqrt(length(x))
x=Hippo_6_9NAACre
Not sure why the graph is producing funky error bars. Can anyone help or provide insight?
I had very recently a similar problem.
To solve it, first of all you may want to remove the layer
geom_errorbar(aes(ymin=meanNAA-NAAse,
ymax=meanNAA+NAAse), width=.2, position=position_dodge(.9))
and rather use a layer with the statsummary function again. That will generate the error bars separated for group.
As you want the bars indicating the standard error, you must create an appropriate function that returns the needed values, such that can be used from statsummary.
Find below a working example with iris dataset.
library(ggplot2)
## create a function for standard error that can be used with stat_summary
# I created the function inspecting the results returned by 'mean_cl_normal' that is the
# function used in some examples of stat_summary (see ?stat_summary).
mean_se = function(x){
se = function(x){sd(x)/sqrt(length(x))}
data.frame(y=mean(x), ymin=mean(x)+se(x), ymax=mean(x)-se(x))
}
## create the plot
p = ggplot(iris, aes(x = Species, y = Sepal.Length), stat="identity") +
stat_summary(fun.y = mean, geom = "col", fill = "White", colour = "Black", width=0.5) +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.2, size=1)
# print the plot
print(p)