In
library(ggplot2)
library(reshape)
df <- as.data.frame(matrix(runif(9),3,3))
df$factor <- letters[1:3]
df.m <- melt(df)
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~factor)
i want to change the facet names. According to the ggplot2 tutorials, this is working:
new.lab <- as_labeller(c(a="A",b="B",c="C"))
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~factor, labeller=new.lab)
However, this is not:
new.lab <- as_labeller(c(a="A",b="B",c=expression(italic("C"))))
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~factor, labeller=new.lab)
How can i get italics (or any other special symbol) in ggplot2 2.0 facets?
How about label_parsed instead?
df.m$f2 <- factor(df.m$factor, labels = c("AAA", "bold(BBB)", "italic(CCC)"))
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~f2, labeller = label_parsed) +
theme(text = element_text(size = 20))
You can specify the type of labeller here using label_parsed,
new.lab <- as_labeller(c(a="A", b="B", c="italic(C)"), label_parsed)
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~factor, labeller = new.lab)
Related
I have a dataframe with two columns variable and value:
df<- data.frame(c(rep('v1',4),rep('v2',4)),c(1,2,3,4,5,6,7,8))
colnames(df) <- c('variable', 'value')
Next I make a boxplot with ggplot2:
library(ggplot2)
j <- ggplot(df, aes(factor(variable), value))
j + geom_boxplot() + facet_wrap(~variable, scale="free")
My problem now is to add with geom_point specific points which were not present in the original dataframe to v1 and v2 boxplots in different (red) colour. How can I do it?
Based on your description, I think this might be what you want.
df<- data.frame(c(rep('v1',4),rep('v2',4)),c(1,2,3,4,5,6,7,8))
colnames(df) <- c('variable', 'value')
df2 <- data.frame(c(rep('v1',4),rep('v2',4)),c(2,5,6,1,2,3,5,3))
colnames(df2) <- c('variable', 'value')
library(ggplot2)
ggplot(df, aes(factor(variable), value)) + geom_boxplot() + facet_wrap(~variable, scale="free") +
geom_point(data = df2,aes(x=variable, y=value),color = 'red')
If you want to add a point to both facets then see the example here:
library(ggplot2)
j <- ggplot(df, aes(factor(variable), value)) +
geom_boxplot() +
facet_wrap(~variable, scales="free")
j +
geom_point(aes(x=1, y=2), col="red", size=3)+
geom_point(aes(x=1, y=2.5), col="green", size=3)
I have the following dataset I would like to plot.
library(tidyverse)
df <- data.frame(first=c(40,40,40),second=c(40,80,160),third=c(40,160,640), ID=c("ID1","ID2","ID3")) %>% pivot_longer(cols=-ID)
I am using:
ggplot(df2, aes(x=name, y=value, group=ID)) +
geom_line() +
geom_point(size=4)
Currently, I have:
Is there a way to change the values shown on the y axis:
1:40
1:80
1:160
1:320
1:640
Basically, I am writing a string for the continues numeric values on y
You can set the breaks= and label= parameters to change your y-axis labels
ggplot(df, aes(x=name, y=value, group=ID)) +
geom_line() +
geom_point(size=4) +
scale_y_continuous(breaks=c(40,80,160,320,640), label=function(x) paste0("1:", x))
library(ggplot2)
library(tidyverse)
data <- as.data.frame(matrix(rnorm(60),20,3))
data$factor <- c(rep("f1", 4),rep("f2", 4), rep("f3", 4), rep("f4",4),rep("f5", 4))
names(data) <- c("A", "B", "C", "factor")
melted <- melt(data)
melted$factor <- factor(melted$factor,
levels=paste0('f', 1:5),
labels=c('beta[sigma]^2=1', 'beta[sigma]^2=2', 'beta[sigma]^2=3', 'beta[sigma]^2=4', 'beta[sigma]^2=5'))
p <- ggplot(melted, aes(variable, value)) +
stat_boxplot(geom ='errorbar') +
geom_boxplot()
p + facet_wrap(~factor, labeller=label_parsed)
Is there a different way of specifying the equal sign in the plot title so that it doesn't have the parentheses around it?
An option is to skip the factor/labels approach and format the factor column with label_bquote
library(ggplot2)
library(dplyr)
melted %>%
mutate(new = readr::parse_number(factor)) %>%
ggplot(aes(variable, value)) +
stat_boxplot(geom = 'errorbar') +
geom_boxplot() +
facet_wrap(~ new, labeller = label_bquote(beta[sigma]^2==.(new)))
-output
If we want to use the OP's option with label_parsed, change the = to == in the labels
melted$factor <- factor(melted$factor,
levels=paste0('f', 1:5),
labels=c('beta[sigma]^2==1',
'beta[sigma]^2==2',
'beta[sigma]^2==3',
'beta[sigma]^2==4',
'beta[sigma]^2==5'))
Now, it should work
p <- ggplot(melted, aes(variable, value)) +
stat_boxplot(geom ='errorbar') +
geom_boxplot()
p +
facet_wrap(~factor, labeller=label_parsed)
-output
I would like to chose specific colors of the colorblind_pal() from ggthemes
This works:
library(ggplot2)
library(ggthemes)
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg,
colour = factor(gear))) + facet_wrap(~am)
p + theme_igray() + scale_colour_colorblind()
Now I would like to chose specific colors of the colorblind_pal() for my plot. How can I chose them?
I tried following with no success:
my_palette <- palette(c("#000000","#F0E442","#D55E00"))
p + theme_igray() + scale_colour_colorblind(my_palette)
You could use scale_color_manual in order to manually specify the colors to use:
library(ggplot2)
library(ggthemes)
p <- ggplot(mtcars) +
geom_point(aes(x = wt, y = mpg, colour = factor(gear))) +
facet_wrap(~am) +
theme_igray() +
scale_color_manual(values = c("#000000","#F0E442","#D55E00"))
p
Since you already have the colors, you can just use scale_color_manual:
library(ggthemes)
library(ggplot2)
COLS=colorblind_pal()(8)
COLS = COLS[c(1,5,7)]
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg,
colour = factor(gear))) + facet_wrap(~am)
p + theme_igray() + scale_colour_manual(values=COLS))
first, sorry for my English and mistakes.
I have a plot like this:
data <- data.frame(site=rep(letters[1:6],each=3), year=rep(2001:2003, 6), nb=round(runif(18, min=20, max=60)), group=c(rep("A",9),rep("B", 6),rep("C",3)))
ggplot(data=data, aes(x= factor(year), y= nb)) +
geom_point() +
facet_wrap(~site)
And I would like to add the other panel "group". In fact I would like to make this graph without empty parts:
ggplot(data=data, aes(x= factor(year), y= nb)) +
geom_point() +
facet_grid(group~site)
Does someone has an idea? Thanks for you help!
#
There is this solution which look like that I want, but I thought there were more simple solution :
plt1 <- ggplot(data=data[data$group=="A",], aes(x= factor(year), y= nb)) +
geom_point() +
ggtitle("A")+
facet_grid(~site)+
xlab("") + ylab("")
plt2 <- ggplot(data=data[data$group=="B",], aes(x= factor(year), y= nb)) +
geom_point() +
ggtitle("B")+
facet_grid(~site)+
xlab("") + ylab("")
plt3 <- ggplot(data=data[data$group=="C",], aes(x= factor(year), y= nb)) +
geom_point() +
ggtitle("C")+
facet_grid(~site)+
xlab("") + ylab("")
library(gridExtra)
grid.arrange(arrangeGrob(plt1,plt2, plt3),
left = textGrob("nb",rot=90))
You can combine the site and group inside the facet_wrap() - so you will have only "full" facets.
ggplot(data=data, aes(x= factor(year), y= nb)) +
geom_point() +
facet_wrap(~site+group)