I am trying to plot three variables and want the units in the axes labels but can't find a way to label them properly in facets with the superscripts.
I've tried as_labeller, label_bquote, expression/paste and changing the original data.
p <- ggplot(data = LST, aes(x = Date, y = Measurements)) +
geom_point((aes(color = parameter)))
p + facet_grid(parameter ~ ., scales = "free_y",
switch="y",labeller=as_labeller(DO~(mg~L^{-1}), Temperature~(°C), Light~
(µmol~m^{-2}~s^{-1}))) +
theme_bw()+ theme(strip.background = element_blank(),
legend.title = element_blank(), strip.placement = "outside",
panel.grid.minor = element_blank()) +
scale_x_datetime()+ ylab(NULL) +ggtitle("Spring 2018") +
scale_colour_manual(values=c('royalblue1', 'springgreen4', 'darkblue')) +
theme(legend.position="none")+
theme(strip.text=element_text(size=10))
Everything I try either labels all facets the same or doesn't place the superscripts. I'm pretty new at ggplot2 so am unsure if what I'm trying will help.
You want labeller = label_parsed. Here's a simple example
mt = mtcars
mt$facets = factor(mt$cyl, labels = c(
"DO~(mg~L^{-1})",
"Temperature~('°C')",
"Light~(µmol~m^{-2}~s^{-1})"))
ggplot(mt, aes(mpg,cyl)) +
geom_point() +
facet_grid(~facets, labeller = label_parsed)
Related
I would like to rename the x-axis labels on this plot using the labels mentioned in "mylabels", but it does not work (it only uses the first label for all the ticks). I guess the problem comes from the 'facet_grid" function as I can rename labels properly when I do not use this function. Any idea how to solve this issue?
mylabels <- c("MRA2x3","MRA2x4","MRB2x3","MRB2x4","whole milk")
ggplot(file, aes(x=as.factor(diet),y=ADG, fill= as.factor(farm))) +
geom_boxplot(width=1,alpha=0.5) +
scale_fill_viridis(discrete = TRUE, alpha=0.6,guide = guide_legend(title = "Farm")) +
labs(y="Average daily gain", x = "Diet") +
scale_y_continuous(breaks=seq(0, max(file$ADG), by=0.2)) +
scale_x_discrete(labels = mylabels) +
theme(text = element_text(size = 18),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
plot.title = element_text(hjust = 0.5),
strip.text.x = element_blank()) +
facet_grid(~ diet, scales = "free_x")
You can add a vector of new names to the facet_grid() statement in stead of the scale_x_discrete (delete that):
facet_grid(~ diet,
scales = "free_x",
labeller = labeller(diet = mylabels))
I remember the vector in labeller has to be a named vector : c("old name1" = "new_name1", "old_name2" = "new_name2", etc) so it maps the old names to the new names.
I have a plot that looks like below. However as you can see facet 4 only has 1 sample but the vertical size is equal to facet 6 which has 4 samples; this makes the plot look really weird since it becomes really thick. I can't use face_grid (facet_grid( cyl~. , scales = "free", space="free" )) at the moment because because then the labels goes to the side but what I really want is to put the labels on top like with facet_wrap as illustrated below. Is there anyway around this? thanks.
temp = head ( mtcars )
temp$car = row.names ( temp )
ggplot( temp , aes(x=car , y=hp, fill=vs )) +
geom_bar(stat = 'identity', position = 'stack') +
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
coord_flip() +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
facet_wrap( ~cyl , ncol=1, scales = "free" )
Not sure if you want this. If so, you can use facet_col from the package ggforce, which allows you to have facet in a single column and also drop the facet ratio (space argument).
library(tidyverse)
library(ggforce)
temp = head (mtcars)
temp$car = row.names(temp)
ggplot(temp, aes(x = car , y = hp, fill = vs)) +
geom_bar(stat = 'identity', position = 'stack') +
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
coord_flip() +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
ggforce::facet_col(cyl ~ ., scales = "free", space = "free")
I had two stacked bar graphs, one for input another for output.
however i want to plot these two graphs in one space.
I attached the sample.
Please help me!! thank you!!
gr_out <- read.csv("graph_out.csv",header=TRUE, sep=",")
library(ggplot2)
library(reshape2)
gr_out.metlt<-melt(gr_out)
head(gr_out.metlt)
gr_out.metlt$DMU.Name <- factor(gr_out.metlt$DMU.Name, levels = gr_out$DMU.Name)
q <- ggplot(gr_out.metlt, aes(x = DMU.Name, y = value),add=TRUE)
q + geom_bar(aes(fill = variable), stat = "identity") + theme_bw() +
theme(axis.text.x = element_text(angle=60, hjust=1),panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
ylab("Cummulative ratio") + xlab("Station") + scale_fill_brewer(palette = "Paired")
##graph
gr_in <- read.csv("graph_in.csv",header=TRUE, sep=",")
library(ggplot2)
library(reshape2)
gr_in.metlt<-melt(gr_in)
head(gr_in.metlt)
gr_in.metlt$DMU.Name <- factor(gr_in.metlt$DMU.Name, levels = gr_in$DMU.Name)
p <- ggplot(gr_in.metlt, aes(x = DMU.Name, y = value))
p + geom_bar(aes(fill = variable), stat = "identity") + theme_bw() +
theme(axis.text.x = element_text(angle=60, hjust=1),panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
ylim(c(0.0, 8.0)) + ylab("Cummulative ratio") + xlab("Station")+ scale_fill_brewer(palette = "Paired")
This is what I want to achieve
I am trying to draw this following graph using ggplot2 package, but somehow the axis won't show up. the ticks are there, just not the axis line. I have used the theme(axis.line=element_line()) function, but it wouldn't work.
Here is my code:
library(ggplot2)
ggplot(data = soepl_randsub, aes(x = year, y =satisf_org, group = id)) +
geom_point() + geom_line() +ylab("Current Life Satisfaction") +theme_bw() +
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() ) +
theme(panel.border= element_blank()) +
theme(axis.line = element_line(color="black", size = "2"))
I am not sure what went wrong. Here is the chart.
The bug was fixed in ggplot2 v2.2.0 There is no longer a need to specify axis lines separately.
I think this is a bug in ggplot2 v2.1.0. (See this bug report and this one.) A workaround is to set the x-axis and y-axis lines separately.
library(ggplot2)
ggplot(data = mpg, aes(x = hwy, y = displ)) +
geom_point() +
theme_bw() +
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() )+
theme(panel.border= element_blank())+
theme(axis.line.x = element_line(color="black", size = 2),
axis.line.y = element_line(color="black", size = 2))
You don't need to specify axis-size for X and Y separately. When you are specifying size="2", R is considering value 2 as non-numeric argument. Hence, axis-line parameter is defaulted to 0 size. Use this line of code:
ggplot(data = mpg, aes(x = hwy, y = displ)) + geom_point() +xlab("Date")+ylab("Value of Home")+theme_bw() +theme(plot.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank()) + theme(panel.border= element_blank()) +
theme(axis.line = element_line(color="black", size = 2))
axis_line inherits from line in R, hence specifying size is mandatory for non-default values.
I'm working with some data that I want to display as a nxn grid of plots. Edit: To be more clear, there's 21 categories in my data. I want to facet by category, and have those 21 plots in a 5 x 5 square grid (where the orphan is by itself on the fifth row). Thus facet_wrap instead of facet_grid.
I've got the following code written up for doing it (using the good old iris data set for my reproducible example):
library(ggplot2)
library(grid)
cust_theme <- theme_bw() + theme(legend.position="none",
axis.title = element_blank(), axis.ticks = element_blank(),
axis.text = element_blank(), strip.text = element_blank(),
strip.background = element_blank(), panel.margin = unit(0, "lines"),
panel.border = element_rect(size = 0.25, color = "black"),
panel.grid = element_blank())
iris.plot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() + cust_theme + facet_wrap( ~ Species, ncol = 2) +
labs(title = "Irises by species")
This gives me ALMOST what I want, but not quite:
I've still got a tiny strip of space between the top row of plots and the bottom row. I'd like to get rid of that entirely, but panel.margin is obviously not doing it. Is there a way to do this?
This might be a little late, but panel.marginis now deprecated. Inside theme use panel.spacing. To eliminate the spacing between the facets then load the grid package and use panel.spacing = unit(0, "lines")
Change the panel.margin argument to panel.margin = unit(c(-0.5,0-0.5,0), "lines"). For some reason the top and bottom margins need to be negative to line up perfectly. Here is the result:
You can also edit the grobs directly:
library(ggplot2)
library(grid)
g <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
facet_wrap( ~ Species, ncol = 2) +
labs(title = "Irises by species") +
theme_bw() +
theme(panel.margin = unit(0, "lines")) +
theme(plot.margin = unit(c(0,0,0,0), "lines")) +
theme(strip.background = element_blank()) +
theme(plot.background = element_blank()) +
theme(strip.text = element_blank()) +
theme(axis.ticks.margin = unit(0, "lines"))
g <- ggplotGrob(p)
g$heights[[7]] = unit(0, "lines")
grid.newpage()
grid.draw(g)