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 have attached multiple plots to one page using grid.arrange.
Is there a way to label each plot with "(a)","(b)" etc...
I have tried using geom_text but it does not seem compatible with my plots....
.... as you can see, geom_text has some strange interaction with my legend symbols.
I will show an example using the mtcars data of what I am trying to achieve. THe alternative to geom_text I have found is "annotate" which does not interact with my legend symbols. However, it is not easy to label only one facet....
q1=ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(a)",size=8,family="serif")
q2=ggplot(mtcars, aes(x=mpg, y=wt,)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(b)",size=8,family="serif")
geom_text(x=15, y=5,size=8, label="(b)")
gt1 <- ggplotGrob(q1)
gt2 <- ggplotGrob(q2)
grid.arrange(gt1,gt2, ncol=1)
Therefore, my question is, is there a way to label plots arranged using grid.arrange, so that the first facet in each plot is labelled with either a, or b or c etc...?
You can use ggarrange from ggpubr package and set labels for each plot using the argument labels:
library(ggplot2)
library(ggpubr)
q1=ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(a)",size=8,family="serif")
q2=ggplot(mtcars, aes(x=mpg, y=wt,)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(b)",size=8,family="serif")
ggarrange(q1,q2, ncol = 1, labels = c("a)","b)"))
Is it what you are looking for ?
If you set inherit.aes=FALSE, you can prevent it from interring:
ggplot(mtcars, aes(x=mpg, y=wt,col=factor(cyl))) +
geom_line() +
geom_point()+
geom_text(inherit.aes=FALSE,aes(x=15,y=12,label="(a)"),
size=8,family="serif")+
facet_grid(~cyl)
If you want to only label the first facet (hope I got you correct), I think the easiest way to specify a data frame, e.g if we want only something in the first,
#place it in the first
lvl_data = data.frame(
x=15,y=12,label="(a)",
cyl=levels(factor(mtcars$cyl))[1]
)
ggplot(mtcars, aes(x=mpg, y=wt,col=factor(cyl))) +
geom_line() +
geom_point()+
geom_text(data=lvl_data,inherit.aes=FALSE,
aes(x=x,y=y,label=label),size=8,family="serif")+
facet_grid(~cyl)
I have noticed that in ggplot when you add a title containing lowercase letters q,y,p,g,j the dimensions of the plot gets modified.
zoom
*the red lines have been added manually
As you can see, the height of the plot gets smaller when I add one of the above letters to the title
How can I keep the height constant between several plots with different titles?
Code used to produce the two plots:
# plot 1
ggplot(mpg, aes(factor(cyl), hwy)) +
geom_point(size=4) +
theme_bw() +
ggtitle("main")
# plot 2
ggplot(mpg, aes(factor(cyl), hwy)) +
geom_point(size=4) +
theme_bw() +
ggtitle("pmain")
you could add a phantom lowercase letter, but that requires your text to be converted into a plotmath expression which may have some downsides, fontwise
.st <- function(s){
bquote(phantom("tp")*.(s))
}
# plot 1
p1 <- ggplot(mpg, aes(factor(cyl), hwy)) +
geom_point(size=4) +
theme_bw() +
ggtitle(.st("main"))
# plot 2
p2 <- ggplot(mpg, aes(factor(cyl), hwy)) +
geom_point(size=4) +
theme_bw() +
ggtitle(.st("pmain"))
grid.arrange(p1,p2,ncol=2)
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?
I used the facetting in ggplot. My question is: how is it possible to draw ticks between facets? I am aware of the scale = "free_y" option which gives ticks and values:
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_wrap( ~ cyl, scale = "free_y")
But only ticks?
This is basically what I want: