I have recently started using ggplot2 so, I once again apologize for posting basic question.
I read about this code in one of the help pages. This code nicely plots line graph and the average with red dot:
ggplot(mpg, aes(trans, cty)) +
geom_point() +
stat_summary(geom = "point", fun.y = "mean", colour = "red", size = 4)
I thought of simplifying this a bit by overriding the contents of geom_point() and using stat="summary"
ggplot(mpg, aes(trans, cty)) +
geom_point(stat = "summary", fun.y = "mean", colour = "red", size = 4) +
However, the above code doesn't work. Can someone please help me why above code doesn't work? Specifically, it only plots the red dot (mean point). I don't see the scatterplot although I have used geom_point()
Because ggplot() defines aes(), but you are overriding them using the geom layer.
In the first case, geom_point() completes the plotting of the points, and stat_summary() did the part of adding summary statistics layer to your graph.
But, when you define stat = "summary", you tell geom_point() to plot summary statistics instead of the data points.
Head over the ggplot2 documentation and read how mapping works within this ecosystem: http://docs.ggplot2.org/current/
Related
I was working on a task where I'm required to find if there is increase in price while increase in number of rooms. I've used ggplot2 and geom_point.
But I'm unable to understand is there any increment. Could any one help to make me understand this graph please. Or is there any other way to draw graph so that I can understand easily.
The following line is my code.
ggplot(df, aes(x = rooms, y = price)) + geom_point()
Try this - it adds a regression line with confidence interval:
ggplot(df, aes(x = rooms, y = price)) +
geom_point() +
geom_smooth(method = "lm")
What you could do to improve presentation of your data is use geom_jitter to make the points overlap less. Perhaps you could tweak transparency, too. If you add geom_violin you could also show the distribution of points. Finally, you can add mean to every level (number of rooms). Something along the lines of
library(ggplot2)
ggplot(mtcars, mapping = aes(x = cyl, y = hp)) +
theme_bw() +
stat_summary(geom = "point", fun.y = mean, aes(group = 1), size = 2, color = "red") +
geom_jitter(width = 0.25)
I have a plot looking principally like this when based on the mpg-dataset:
library(datasets)
plot2 <- ggplot(mapping = aes(
x = cty,
y = hwy,
group = as.factor(cyl),
shape = as.factor(cyl),
linetype = as.factor(cyl)),
data = mpg) +
geom_point() +
geom_smooth(method = lm, se = F, color = "black") +
theme(legend.key.width = unit(4,"cm"))
plot2
I would like to be able to control the size of the symbols in the legend without affecting the thickness of the lines. Trying with the idea of using override.aes from similar threads gives bigger symbols but at the same time thicker lines.
plot2 + guides(shape = guide_legend(override.aes = list(size=5)))
This question is partly described in Modifying legends in ggplot2 with interactions and guides; however, the question in the link adresses changes to Geom_Path and not Geom_Smooth and does not explain how to find the exact definition of the ggproto()-object to change. It would be helpful if someone could supply this information - then I could probably modify the code myself in a similar fashion.
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)
I have created a faceted boxplot using the ggplot2 package. The R code is as follows:
version.labs <- c(`1`="Version 1.0", `2`="Version 2.0", `3`="Version 3.0", `4`="Version 4.0", `5`="Version 5.0")
ggplot(df, aes(x=factor(Subsystem), y=Risk.value, fill=factor(Version)) ) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(Version)), alpha=0.9) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) + facet_grid(.~Version, labeller = as_labeller(version.labs)) +
theme(strip.text.x = element_text(size=9, color="black", face="bold"))
The resulting plot looks pretty good (as shown below) exept for the legend.
In the legend I want to change the title as well as the text label for each item. The title should be "Version" and the labels "Version 1.0", ..., "Version 5.0".
I've tried various ways but they all add a new separate legend. The new legend looks good, but the old one is still there which doesn't look good and I can't find a way to remove it.
The last thing I tried was to add the scale_color_manual() function, like this:
scale_color_manual(name = "Version", labels=c("v1.0","v2.0","v3.0","v4.0","v5.0"), values=c("grey","blue","green","red","black"))
This results in a boxplot that looks like this.
As can be seen there are two legends. So, close but no cigar. Any hints on how to solve this are appreciated.
I figured out the problem. It was that I had placed my aestetic fill function aes() in the general ggplot(). This should instead be placed in geom_boxplot(), otherwise both the general ggplot() as well as the geom_boxplot() will add a legend. So I fixed that, and then I used guides() to update the title in the geom_boxplot() legend. The full code looks as follows:
ggplot(df, aes(x=factor(Subsystem), y=Risk.value) ) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(Version)), alpha=0.9) +
geom_boxplot(alpha = 0.5, show.legend = FALSE, aes(fill=factor(Version))) + facet_grid(.~Version, labeller = as_labeller(version.labs)) +
theme(strip.text.x = element_text(size=9, color="black", face="bold")) +
labs(x="Risk distribution per software version and subsystem type", y="Normalized risk value") +
guides(color=guide_legend("Version"))
The final plot looks like this, which I'm happy with.
You are using fill argument for grouping and generation of legend. may be instead of scale_color_manual you can use scale_fill_manual to override the existing legend
I am trying to change the style settings of this kind of chart and hope you can help me.
R code:
set_theme(theme_bw)
cglac$pred2<-as.factor(cglac$pred)
ggplot(cglac, aes(x=depth, colour=pred2))
+ geom_bar(aes(y=..density..),binwidth=3, alpha=.5, position="stack")
+ geom_density(alpha=.2)
+ xlab("Depth (m)")
+ ylab("Counts & Density")
+ coord_flip()
+ scale_x_reverse()
+ theme_bw()
which produces this graph:
Here some points:
What I want is to have the density line as black and white lines separated by symbols rather than colour (dashed line, dotted line etc).
The other thing is the histogram itself. How do I get rid of the grey background in the bars?
Can I change the bars also to black and white symbol lines (shaded etc)? So that they would match the density lines?
Last but not least I want to add a second x or in this case y axis, because of flip_coord(). The one I see right now is for the density. The other one I need would then be the count data from the pred2 variable.
Thanks for helping.
Best,
Moritz
Have different line types: inside aes(), put linetype = pred2. To make the line color black, inside geom_density, add an argument color = "black".
The "background" of the bars is called "fill". Inside geom_bar, you can set fill = NA for no fill. A more common approach is to fill in the bars with the colors, inside aes() specify fill = pred2. You might consider faceting by your variable, + facet_wrap(~ pred2, nrow = 1) might look very nice.
Shaded bars in ggplot? No, you can't do that easily. See the answers to this question for other options and hacks.
Second y-axis, similar to the shaded symbol lines, the ggplot creator thinks a second y-axis is a terrible design choice, so you can't do it at all easily. Here's a related question, including Hadley's point of view:
I believe plots with separate y scales (not y-scales that are transformations of each other) are fundamentally flawed.
It's definitely worth considering his point of view, and asking yourself if those design choices are really what you want.
Different linetypes for densities
Here's my built-in data version of what you're trying to do:
ggplot(mtcars, aes(x = hp,
linetype = cyl,
group = cyl,
color = cyl)) +
geom_histogram(aes(y=..density.., fill = cyl),
alpha=.5, position="stack") +
geom_density(color = "black") +
coord_flip() +
theme_bw()
And what I think you should do instead. This version uses facets instead of stacking/colors/linetypes. You seem to be aiming for black and white, which isn't a problem at all in this version.
ggplot(mtcars, aes(x = hp,
group = cyl)) +
geom_histogram(aes(y=..density..),
alpha=.5) +
geom_density() +
facet_wrap(~ cyl, nrow = 1) +
coord_flip() +
theme_bw()