How to put the y axis label over 2 lines ggplot - r

I am trying to make the y axis of the second plot over 2 lines. Using '\n' for the first plot worked fine but using it on the second makes the text in odd places (maybe because of the italics).
p1 <- ggplot(data = new_data) +
geom_line(mapping = aes(x = Date,
y = Proportion,
group = Species,
colour = Species)) +
scale_colour_manual(values=c(Golden_Trevally="goldenrod2",
Red_Snapper="firebrick2",
Sharksucker_Remora="darkolivegreen3",
Juvenile_Remora="aquamarine2")) +
xlab("Date (2014-2018)") +
ylab("Total Presence \n Per Month ") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
theme(legend.position="top") +
labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date, Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') +
xlab("Date (2014-2018)") +
ylab("Total Number of "~italic(\nM.alfredi)~" Encounters") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
grid.arrange(p1,p2)

You can try this:
geom_line(mapping = aes(x = Date,
y = Proportion,
group = Species,
colour = Species)) +
scale_colour_manual(values=c(Golden_Trevally="goldenrod2",
Red_Snapper="firebrick2",
Sharksucker_Remora="darkolivegreen3",
Juvenile_Remora="aquamarine2")) +
xlab("Date (2014-2018)") +
ylab("Total Presence \n Per Month ") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
theme(legend.position="top") +
labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date, Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') +
labs(x="Date (2014-2018)",
y=expression(atop(paste("Total Number of"), paste(italic("M.alfredi"), " Encounters")))) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
grid.arrange(p1,p2)

You need space between \n and M.alfredi in p2. Since there is no reproducible example, here is my suggestion for the second plot,
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') +
xlab("Date (2014-2018)") +
ylab("Total Number of "~italic(\n M.alfredi)~" Encounters") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
It should solve your problem.

Related

Add totals on top of each bar

I am plotting my data and want to display totals on top of each bar but as soon as I add total count the bars disappear.
long<- data.frame(
Name = c("abc","abc","abc","gif","gif","gif","xyz","xyz","xyz"),
variable = c("a","b","c","a","b","c","c","b","a"),
value = c(4,6,NA,2,8,1,6,NA,NA))
Code
p<-long %>%
ggplot() + aes(Name, value, fill=variable) +
geom_bar(stat="summary", position = "fill") +
scale_y_continuous(labels = scales::percent_format()) +
ylab("Total_num") +
ggtitle("Totalnum") +
theme(plot.title = element_text(size = 20, hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 75, vjust = 0.95, hjust=1))
p+ stat_summary(fun.y = sum, aes(label = ..y.., group = Name)+
geom_text(stat='value', aes(group=Name, label=.."value"..), position = position_stack(vjust = 0.5))
You can achieve that creating another df with the sum of value per Name and passing this to geom_text()
long<- data.frame(
Name = c("abc","abc","abc","gif","gif","gif","xyz","xyz","xyz"),
variable = c("a","b","c","a","b","c","c","b","a"),
value = c(4,6,NA,2,8,1,6,NA,NA))
long_totals <- long %>%
group_by(Name) %>%
summarise(Total = sum(value, na.rm = T))
p <- ggplot()+
geom_bar(data = long,
aes(x = Name,
y = value,
fill=variable),
stat="summary",
position = "fill") +
geom_text(data = long_totals,
aes(y = 100,
x = Name,
label = Total),
size = 7,
position = position_fill(vjust = 1.02)) +
scale_y_continuous(labels = scales::percent_format()) +
ylab("Total_num") +
ggtitle("Totalnum") +
theme(plot.title = element_text(size = 20, hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 75, vjust = 0.95, hjust=1))

Color for facet grid, based on x-axis, in ggplot2

I have color for each variable (fishing strategy), however, if I put it in a facet grid like this, based on the years, I can't set up the colors accordingly. I want to have one color for each fishing strategy instead of one color for each year, but also need the legend for fishing strategies with color or just years without color. But I didn't manage to do that. Can someone help me with this?
With this code:
spaclu <- ggplot(io1, aes(y= effort, x=factor(clu_name2), fill= factor(year))) + geom_bar(stat="identity", position="dodge")
+ theme_minimal()
spaclu + facet_grid(vessel_category~geartype_clu2, scales = "free")
+ labs(fill = "Year", x = "Fishing strategies", y = "Total REA", title = "Based on the REA")
+ theme(text = element_text(size = 13))
+ theme(legend.position = "bottom")
+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+
scale_fill_manual(values = c("GIL_COD" = "#004c6d",
"GIL_FRS" = "#00ffff",
"GIL_FLE" = "#00a1c1",
"GIL_HER" = "#00cfe3",
"PAS_FLA" = "#78ab63",
"POL_FRS" = "#6efa75",
"BST_MIX" = "#ffc334",
"MPT_HER" = "#ff9509",
"BPT_HER" = "#ffb6de",
"BPT_COD" = "#cc0089"))
I get this
but if I removed the scale fill manual part, it looked like this
I think I've worked it out, but I don't have your data. (It's a bit messy.)
I used the dataset diamonds and renamed the fields. The first plot is supposed to represent your second plot, where you have removed the scale_color_manual.
The data first:
library(tidyverse)
# create variables
io1 <- diamonds %>%
mutate(year = cut,
effort = x,
clu_name2 = color,
vessel_category = rep(c("Passive","Active"), nrow(diamonds)/2),
geartype_clu2 = rep(LETTERS[1:3], nrow(diamonds)/3))
levels(io1$year) <- c(2019:2023)
Original plot as you've coded:
# grid faceting/color
spaclu <- ggplot(io1, aes(y= effort, x=factor(clu_name2), fill= factor(year))) +
geom_col(position = "dodge") +
theme_minimal()
spaclu + facet_grid(vessel_category~geartype_clu2, scales = "free") +
labs(fill = "Year", x = "Fishing strategies", y = "Total REA",
title = "Based on the REA") +
theme(text = element_text(size = 13)) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
The primary differences are the arguments group = year and fill = clu_name2.
p2 <- ggplot(io1, aes(x = clu_name2, y = effort, group = year, fill = clu_name2)) +
geom_col(position = "dodge") +
theme_minimal()
p2 + facet_grid(vessel_category~geartype_clu2, scales = "free") +
labs(fill = "", x = "Fishing strategies\ngrouped by years",
y = "Total REA", title = "Based on the REA") +
theme(text = element_text(size = 13)) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
Keep in mind the legend here is fishing strategies. If you want the years shown, perhaps a second depth label with scale_fill_manual() would be a good approach.
Now as far getting the years and the strategies in the legend or as legends. You may be better off with adding a second scale axis and using the first version. This can be done with the package ggnewscale. You'll have to adjust the font size or expand or add to the margin, though.
# grid faceting/color
spaclu <- ggplot(io1, aes(y= effort, x=factor(clu_name2), fill= factor(year),
group = year)) +
geom_col(position = "dodge") +
theme_minimal()
spaclu + facet_grid(vessel_category~geartype_clu2, scales = "free") +
labs(fill = "Year", x = "Fishing strategies", y = "Total REA",
title = "Based on the REA") +
theme(text = element_text(size = 13)) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
ggnewscale::new_scale("fill") + # added scale here
geom_tile(aes(fill = clu_name2, y = 1)) +
scale_fill_discrete(name = "Strategies")
It doesn't quite work out when using it with the second option
p2 <- ggplot(io1, aes(x = clu_name2, y = effort, group = year, fill = clu_name2)) +
geom_col(position = "dodge") +
theme_minimal()
p2 + facet_grid(vessel_category~geartype_clu2, scales = "free") +
labs(fill = "", x = "Fishing strategies\ngrouped by years",
y = "Total REA", title = "Based on the REA") +
theme(text = element_text(size = 13)) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
scale_fill_manual(values = c("D" = "#004c6d",
"E" = "#00ffff",
"F" = "#00a1c1",
"G" = "#00cfe3",
"H" = "#78ab63",
"I" = "#6efa75",
"J" = "#ffc334",
"K" = "#ff9509",
"L" = "#ffb6de",
"M" = "#cc0089")) +
ggnewscale::new_scale("fill") +
geom_tile(aes(fill = year, y = 1)) +
scale_fill_viridis_d(name = "Years")

How to change y axis from count to prop?

With ggplot2 and GGally, I created this bar chart with proportions:
ggplot(mtcars, aes(x = factor(cyl), by = 1)) +
geom_bar(fill = "steelblue", stat = "prop") +
geom_text(aes(label = scales::percent(after_stat(prop), accuracy = 1)), stat = "prop", nudge_y = 0.5) +
theme_minimal() +
theme(aspect.ratio = 1.5)
However, on the y axis, I would like to change that to reflect the percentages on the bars. I would like to avoid hard coding the values like ylim = "40", but let it use the values in the chart.
Try this:
ggplot(mtcars, aes(x = cyl)) +
geom_bar(aes(y = ..prop..), fill = "steelblue", stat = "count") +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), stat= "count", vjust = -.5) +
ylim(0, 0.5) +
ylab("") +
theme_minimal() +
theme(aspect.ratio = 1.5)
Edit: if you want a factor on x axis try
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar(aes(y = (..count..)/sum(..count..)), fill = "steelblue", stat = "count") +
geom_text(aes(label = scales::percent(round((..count..)/sum(..count..), 2)),
y = ((..count..)/sum(..count..))), stat = "count", vjust = -.25) +
ylim(0, 0.5) +
ylab("") +
theme_minimal() +
theme(aspect.ratio = 1.5)
Edit2: with the GGally package you can use:
ggplot(mtcars, aes(x = factor(cyl), by = 1)) +
geom_bar(aes(y = ..prop..), fill = "steelblue", stat = "prop") +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), stat = "prop", vjust = -.5) +
ylim(0, 0.5) +
ylab("") +
theme_minimal() +
theme(aspect.ratio = 1.5)

Legend doesn't go to the bottom

I've copy a code of another user and am adapting it to my data.
The code:
library(gridExtra)
library(grid)
library(ggplot2)
x <- data.frame(
date = seq(as.Date("2012-01-01"),as.Date("2012-12-31"), by="week"),
rain = sample(0:20,53,replace=T),
flow1 = sample(50:150,53,replace=T),
flow = sample(50:200,53,replace=T))
g.top <- ggplot(x, aes(x = date, y = rain, ymin=0, ymax=rain)) +
geom_linerange() +
scale_y_continuous(limits=c(22,0),expand=c(0,0), trans="reverse")+
theme_classic() +
theme(plot.margin = unit(c(5,5,-32,6),units="points"),
axis.title.y = element_text(vjust = 0.3))+
labs(y = "Rain (mm)")
g.bottom <- ggplot(x, aes(x = date)) +
geom_line(aes(y = flow, colour = "flow")) +
geom_line(aes(y = flow1, colour = "flow1")) +
theme(legend.position="bottom") +
theme_classic() +
theme(plot.margin = unit(c(0,5,1,1),units="points")) +
labs(x = "Date", y = "River flow (m/s)")
grid.arrange(g.top, g.bottom , heights = c(1/5, 4/5))
I wanted that the legend went to the bottom, but it doesn't go.
enter image description here
try this:
library(gridExtra)
library(grid)
library(ggplot2)
x <- data.frame(
date = seq(as.Date("2012-01-01"),as.Date("2012-12-31"), by="week"),
rain = sample(0:20,53,replace=T),
flow1 = sample(50:150,53,replace=T),
flow = sample(50:200,53,replace=T))
g.top <- ggplot(x, aes(x = date, y = rain, ymin=0, ymax=rain)) +
geom_linerange() +
scale_y_continuous(limits=c(22,0),expand=c(0,0), trans="reverse")+
theme_classic() +
theme(plot.margin = unit(c(5,5,-32,6),units="points"),
axis.title.y = element_text(vjust = 0.3))+
labs(y = "Rain (mm)")
g.bottom <- ggplot(x, aes(x = date)) +
geom_line(aes(y = flow, colour = "flow")) +
geom_line(aes(y = flow1, colour = "flow1")) +
theme_classic() +
# here the mod
theme(plot.margin = unit(c(0,5,1,1),units="points"),legend.position="bottom") +
labs(x = "Date", y = "River flow (m/s)")
grid.arrange(g.top, g.bottom , heights = c(1/5, 4/5))

Interaction Plot in ggplot2

I'm trying to make interaction plot with ggplot2. My code is below:
library(ggplot2)
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw()
p <- p + labs(x="Dose", y="Response")
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue")
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = 1))
p <- p + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0))
p <- p + opts(axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
print(p)
How can I plot dose-supp level combination means rather than only dose level means which I'm getting here? Thanks in advance for your help.
You can precalculate the values in their own data frame:
toothInt <- ddply(ToothGrowth,.(dose,supp),summarise, val = mean(len))
ggplot(ToothGrowth, aes(x = factor(dose), y = len, colour = supp)) +
geom_boxplot() +
geom_point(data = toothInt, aes(y = val)) +
geom_line(data = toothInt, aes(y = val, group = supp)) +
theme_bw()
Note that using ggplot rather than qplot makes the graph construction a lot clearer for more complex plots like these (IMHO).
You can compute your summaries by the appropriate groups (supp):
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw()
p <- p + labs(x="Dose", y="Response")
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue", aes(group=supp))
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = supp))
p <- p + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0))
p <- p + opts(axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
print(p)
Or converting to ggplot syntax (and combining into one expression)
ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) +
geom_boxplot() +
stat_summary(aes(group=supp), fun.y = mean, geom="point", colour="blue") +
stat_summary(aes(group=supp), fun.y = mean, geom="line") +
scale_x_discrete("Dose") +
scale_y_continuous("Response") +
theme_bw() +
opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0),
axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
EDIT:
To make this work with 0.9.3, it effectively becomes Joran's answer.
library("plyr")
summ <- ddply(ToothGrowth, .(supp, dose), summarise, len = mean(len))
ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) +
geom_boxplot() +
geom_point(data = summ, aes(group=supp), colour="blue",
position = position_dodge(width=0.75)) +
geom_line(data = summ, aes(group=supp),
position = position_dodge(width=0.75)) +
scale_x_discrete("Dose") +
scale_y_continuous("Response") +
theme_bw() +
theme(axis.title.x = element_text(size = 12, hjust = 0.54, vjust = 0),
axis.title.y = element_text(size = 12, angle = 90, vjust = 0.25))
If you think you might need a more general approach, you could try function rxnNorm in package HandyStuff (github.com/bryanhanson/HandyStuff). Disclaimer: I'm the author. Disclaimer #2: the box plot option doesn't quite work right, but all the other options are fine.
Here's an example using the ToothGrowth data:
p <- rxnNorm(data = ToothGrowth, res = "len", fac1 = "dose", fac2 = "supp", freckles = TRUE, method = "iqr", fac2cols = c("red", "green"))
print(p)
a much easier way. without ddply. directly with ggplot2.
ggplot(ToothGrowth, aes(x = factor(dose) , y=len , group = supp, color = supp)) +
geom_boxplot() +
geom_smooth(method = lm, se=F) +
xlab("dose") +
ylab("len")

Resources