I would like to change the colours of the strip backgrounds to a predefined order.
This code generates the plot, and changes the strip backgrounds to red:
p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(. ~ cyl) +
theme(strip.background = element_rect(fill="red"))
I'd like to do something like the below however, which ideally would specify a different colour for each strip
p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(. ~ cyl) +
theme(strip.background = element_rect(fill=c("red","green","blue","yellow")))
Which just makes them all red...
This was asked in similar questions years ago, the answer was to manipulate grobs. I was hoping that there was a simpler solution in the years since?
Related
is there any way i could add a vertical line in both the plot at x=15. and also add a plotly to this. i tried but it doesn't seem to work. Thanks
sbucks_new %>%
ggplot(aes(x= category, y= bad_fat, color= category)) +
geom_boxplot() +
coord_flip() +
facet_grid(~ milk_dummy)+
labs(title= "Unhealthy Fats in Milk drinks by Category",
x= "Drinks Category",
y="Bad Fats (g)") +
theme_bw()
Use geom_hline for plotting the vertical line (confusing due to the coord_flip).
Here's an example with mtcars:
p <- ggplot(mtcars, aes(x=factor(carb), y=disp)) +
geom_boxplot() +
facet_wrap(~am2) +
geom_hline(aes(yintercept=300)) +
coord_flip()
Not sure about your other question, but you can quickly convert ggplot object into plotly using ggplotly function.
plotly::ggplotly(p)
I came across this issue while analyzing my data and I was able to replicate it with the example of the official ggplot reference.
This code creates black points that seem to be the original points before jitter was applied with collors:
ggplot(mpg, aes(cyl, hwy)) +
geom_point() +
geom_jitter(aes(colour = class))
However, this code works fine, it doesn't show the black points:
p <- ggplot(mpg, aes(cyl, hwy))
p + geom_point()
p + geom_jitter(aes(colour = class))
I was thinking it may be related to geom_point printing the black dots before geom_jitter, but if this is the case, why does it work fine in the second example, which follows the same order?
This is the image of the black points
geom_jitter is merely a convenience function, it is calling geom_point under the hood. Because of this, your use of p + geom_point() + geom_jitter(aes(color=class)) is actually the same as
ggplot(ggplot2::mpg, aes(cyl, hwy)) +
geom_point() +
geom_point(aes(color = class), position = "jitter")
which is plotting the same points twice. You can clarify this a little by changing the color of the original points:
ggplot(ggplot2::mpg, aes(cyl, hwy)) +
geom_point(color = "red") +
geom_jitter(aes(color = class))
If you want jittered points, use either geom_point(position = "jitter") or geom_jitter(), not both.
In a faceted plot from ggplot2 package it is possible to change the position of tick marks by the functions like scale_x_continuous(position="top"). However, if tick marks are to be placed on top, they appear above faceting panels, as here:
library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() +
facet_grid(. ~ cyl) +coord_flip() +
scale_y_continuous(position="right")
Is it possible to place individual tick marks scales under the denotations of facets?
How about pushing the facet panel to the bottom?
library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() +
facet_grid(. ~ cyl, switch='x') +coord_flip() +
scale_y_continuous(position="right")
until now I can't find an appropriate answer, here is my short question about ggplot2 in R:
data(mtcars)
ggplot(data=mtcars, aes(x=mpg, y=wt, fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue"))+
geom_point(size=2, pch=21)+
facet_grid(.~cyl)
This is all fine, now I want all data points (regardless what number cyl has) in every facet (e.g. with a smooth grey below the points)?
Thanks, Michael
Here is a slight simplification that makes life a bit easier. The only thing you need to do is to remove the faceting variable from the data provided to the background geom_point() layer.
library(tidyverse)
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
geom_point(data=select(mtcars,-cyl), colour="grey") +
geom_point(size=2, pch=21, aes(fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue")) +
facet_wrap(~cyl)
Using the link given by #beetroot, I was able to do something like this :
g1 <- ggplot(data=mtcars, aes(x=mpg, y=wt)) +
geom_point(data=mtcars[, c("mpg", "wt")], aes(x=mpg, y=wt), colour="grey") +
geom_point(size=2, pch=21, aes(fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue")) +
facet_wrap(~cyl)
This produces the plot :
Hope this helps you.
I trying to remove the little a in front of a legend but without any luck. Other possibility would be to create a legend or legend like text next to the graph but I am running out of ideas. Maybe someone can help me.
I plot on specific positions a red X and I want to point out, that the X marked things are imputed...
df <- data.frame(x=rnorm(10),y=rnorm(10))
ggplot(df, aes(x=x, y=y)) + geom_point() + geom_text(aes(x=0,y=0, color=factor(1)), label='X') +
scale_color_manual(values = 'red', name='imputed',labels='imputed') +
theme(legend.key=element_blank(), legend.title=element_blank())
I think the best result would be to replace the little a by a X. But I could not find any solution for it.
The problem is that you use geom_text to draw the cross.
A simple way to solve it is to use geom_point to plot the cross:
ggplot(df, aes(x=x, y=y)) + geom_point() + geom_point(aes(x=0,y=0, color=factor(1)), shape='X', size=5) +
scale_color_manual(values = 'red',labels='imputed') +
theme(legend.key=element_blank(), legend.title=element_blank())