R Markdown output of bar plot different than output in the console - r
I am trying to make an R Markdown document where a bar plot of urchin abundance and the associated analyses are printed.
I have written code to do this and running the code in the console results in the following figure which is what I want:
However, when I create a code chunk with this code and run it as an R Markdown with an html output, I get a figure that looks like this:
Note that the code for the first figure is in the R Markdown code chunk under the "# combined plot #" section
Also note that the beginning of this chunk is as follows: {r, results='hide', echo = FALSE, message=FALSE, warning=FALSE}
Urchins R Markdown Code
### Packages
library(tidyverse)
library(ggplot2)
library(lme4)
library(lmerTest)
library(emmeans)
library(MuMIn)
library(effects)
library(rcompanion)
library(png)
## Urchin Database
# database with starting urchins #
mean_urchin_totals2 <- structure(list(Year = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L
), Date = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L,
1L, 1L, 1L, 1L, 1L, 1L, 4L, 5L, 5L, 6L, 6L, 7L, 8L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("1/30/18", "11/28/17",
"12/3/17", "2/24/18", "3/10/18", "3/13/18", "3/23/18", "5/15/18",
"5/20/18", "5/25/17", "6/6/17", "9/10/17", "9/5/17"), class = "factor"),
Reef.. = c(211L, 212L, 213L, 214L, 215L, 216L, 111L, 112L,
113L, 114L, 115L, 116L, 111L, 112L, 113L, 114L, 115L, 116L,
211L, 212L, 213L, 214L, 215L, 216L, 111L, 112L, 113L, 114L,
115L, 116L, 211L, 212L, 213L, 214L, 215L, 216L, 211L, 212L,
213L, 214L, 215L, 216L, 116L, 113L, 114L, 111L, 112L, 115L,
211L, 212L, 213L, 214L, 215L, 216L, 111L, 112L, 113L, 114L,
115L, 116L), Site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("HAN", "WAI"), class = "factor"),
Treatment = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L), .Label = c("CLO", "OPE"), class = "factor"),
TimeStep = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L), Site_long = c("Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Waikiki", "Waikiki", "Waikiki", "Waikiki", "Waikiki", "Waikiki",
"Waikiki", "Waikiki", "Waikiki", "Waikiki", "Waikiki", "Waikiki",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki",
"Waikiki", "Waikiki", "Waikiki", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki",
"Waikiki", "Waikiki", "Waikiki", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Waikiki", "Waikiki", "Waikiki", "Waikiki", "Waikiki", "Waikiki"
), Treatment_long = c("Open", "Closed", "Open", "Closed",
"Open", "Closed", "Open", "Closed", "Open", "Closed", "Open",
"Closed", "Open", "Closed", "Open", "Closed", "Open", "Closed",
"Open", "Closed", "Open", "Closed", "Open", "Closed", "Open",
"Closed", "Open", "Closed", "Open", "Closed", "Open", "Closed",
"Open", "Closed", "Open", "Closed", "Open", "Closed", "Open",
"Closed", "Open", "Closed", "Closed", "Open", "Closed", "Open",
"Closed", "Open", "Open", "Closed", "Open", "Closed", "Open",
"Closed", "Open", "Closed", "Open", "Closed", "Open", "Closed"
), Shelter = c("High", "Low", "High", "Low", "High", "Low",
"High", "Low", "High", "Low", "High", "Low", "High", "Low",
"High", "Low", "High", "Low", "High", "Low", "High", "Low",
"High", "Low", "High", "Low", "High", "Low", "High", "Low",
"High", "Low", "High", "Low", "High", "Low", "High", "Low",
"High", "Low", "High", "Low", "Low", "High", "Low", "High",
"Low", "High", "High", "Low", "High", "Low", "High", "Low",
"High", "Low", "High", "Low", "High", "Low"), mean_urchin_abundance = c(25L,
2L, 3L, 6L, 44L, 5L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L, 47L, 15L, 47L, 7L, 67L, 3L, 0L, 0L, 1L, 0L, 0L,
0L, 30L, 6L, 10L, 6L, 48L, 2L, 12L, 1L, 2L, 1L, 10L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 1L, 4L, 0L, 1L, 16L, 0L, 0L, 0L,
0L, 0L, 0L, 0L)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -60L), groups = structure(list(
Year = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L),
Date = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
13L, 13L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 5L, 5L, 6L, 6L, 7L,
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("1/30/18",
"11/28/17", "12/3/17", "2/24/18", "3/10/18", "3/13/18", "3/23/18",
"5/15/18", "5/20/18", "5/25/17", "6/6/17", "9/10/17", "9/5/17"
), class = "factor"), Reef.. = c(211L, 212L, 213L, 214L,
215L, 216L, 111L, 112L, 113L, 114L, 115L, 116L, 111L, 112L,
113L, 114L, 115L, 116L, 211L, 212L, 213L, 214L, 215L, 216L,
111L, 112L, 113L, 114L, 115L, 116L, 211L, 212L, 213L, 214L,
215L, 216L, 211L, 212L, 213L, 214L, 215L, 216L, 116L, 113L,
114L, 111L, 112L, 115L, 211L, 212L, 213L, 214L, 215L, 216L,
111L, 112L, 113L, 114L, 115L, 116L), Site = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("HAN",
"WAI"), class = "factor"), Treatment = structure(c(2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("CLO",
"OPE"), class = "factor"), TimeStep = c(5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), Site_long = c("Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Waikiki", "Waikiki", "Waikiki", "Waikiki",
"Waikiki", "Waikiki", "Waikiki", "Waikiki", "Waikiki", "Waikiki",
"Waikiki", "Waikiki", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki",
"Waikiki", "Waikiki", "Waikiki", "Waikiki", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki",
"Waikiki", "Waikiki", "Waikiki", "Waikiki", "Hanauma Bay",
"Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Hanauma Bay", "Waikiki", "Waikiki", "Waikiki", "Waikiki",
"Waikiki", "Waikiki"), Treatment_long = c("Open", "Closed",
"Open", "Closed", "Open", "Closed", "Open", "Closed", "Open",
"Closed", "Open", "Closed", "Open", "Closed", "Open", "Closed",
"Open", "Closed", "Open", "Closed", "Open", "Closed", "Open",
"Closed", "Open", "Closed", "Open", "Closed", "Open", "Closed",
"Open", "Closed", "Open", "Closed", "Open", "Closed", "Open",
"Closed", "Open", "Closed", "Open", "Closed", "Closed", "Open",
"Closed", "Open", "Closed", "Open", "Open", "Closed", "Open",
"Closed", "Open", "Closed", "Open", "Closed", "Open", "Closed",
"Open", "Closed"), .rows = list(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L)), row.names = c(NA,
-60L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
mean_urchin_totals2$new_date <- strptime(as.character(mean_urchin_totals2$Date), "%m/%d/%y")
mean_urchin_totals2$new_date <- as.Date(mean_urchin_totals2$new_date)
mean_urchin_totals2$Shelter <- ifelse(mean_urchin_totals2$Treatment_long == "Closed", "Low", "High")
mean_urchin_totals2$Shelter <- factor(mean_urchin_totals2$Shelter, levels = c("Low", "High"))
mean_urchin_totals2$Site_long <- as.factor(mean_urchin_totals2$Site_long)
mean_urchin_totals2$Shelter <- as.factor(mean_urchin_totals2$Shelter)
mean_urchin_totals2$Year <- as.factor(mean_urchin_totals2$Year)
## Urchin Analyses
### Distribution
# variables
module_urchin2 <- mean_urchin_totals2$Reef..
plotNormalHistogram(mean_urchin_totals2$mean_urchin_abundance, main = "Urchins")
### glmer with Poisson Distribution
mean_urchin_totals2$Shelter <- factor(mean_urchin_totals2$Shelter, levels = c("High", "Low"))
mean_urchin_totals2$Shelter <- sort(mean_urchin_totals2$Shelter, decreasing = FALSE)
# with EUME, ECMA #
urchin_glmer <- glmer(mean_urchin_abundance ~ Site_long + Shelter + Site_long*Shelter + (1|module_urchin2), data = mean_urchin_totals2, family = poisson, na.action = "na.fail")
summary(urchin_glmer)
# Effects plots #
plot(allEffects(urchin_glmer))
# Residuals glmer #
qqnorm(resid(urchin_glmer), main = "Urchin Residual Plot")
qqline(resid(urchin_glmer))
# Pairwise multiple comparisons #
emm_urchin <- emmeans(urchin_glmer, ~Site_long*Shelter)
pairs(emm_urchin, simple = "each")
### Best Model
# Best model #
dredge(urchin_glmer, extra = "R^2")
urchin_glmer_1 <- glmer(mean_urchin_abundance ~ Site_long + Shelter + Year + (1|module_urchin2), data = mean_urchin_totals2, family = poisson, na.action = "na.fail")
summary(urchin_glmer_1)
# Effects plots #
plot(allEffects(urchin_glmer_1))
# Residuals glmer #
qqnorm(resid(urchin_glmer_1), main = "Urchin Residual Plot")
qqline(resid(urchin_glmer_1))
# emmeans package #
emm <- emmeans(urchin_glmer_1, ~ Site_long*Shelter)
pairs(emm)
## Urchin Plots
### Summary Database
#barplot CI
std.dev.pop <- function(x) sqrt(sum((x - mean(x))^2)/(length(x)))
std.error.pop <- function(x) (std.dev.pop(x))/(sqrt(length(x)))
plot_data_Site4 <- mean_urchin_totals2 %>%
group_by(Site_long, Treatment_long, Shelter) %>%
summarise(mean = mean(mean_urchin_abundance),
sd = std.dev.pop(mean_urchin_abundance),
lower = mean(mean_urchin_abundance) - 1.96*std.error.pop(mean_urchin_abundance),
upper = mean(mean_urchin_abundance) + 1.96*std.error.pop(mean_urchin_abundance))
# reorder summary dataframe for plotting #
plot_data_Site4 <- plot_data_Site4[c(3,4,1,2),]
plot_data_Site4$Shelter <- factor(plot_data_Site4$Shelter, levels = c("Low", "High"))
plot_data_Site4$Site_long <- factor(plot_data_Site4$Site_long, levels = c("Waikiki", "Hanauma Bay"))
# ggplot2 barplot final #
position <- c("Waikiki", "Hanauma Bay")
### Plots
# plot #
urchin_plot <- ggplot(data = plot_data_Site4, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_bar(position = "dodge", stat="identity", width = .8) +
scale_x_discrete(limits = position) +
scale_y_continuous(breaks = seq(0, 30, 5)) +
geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = "Mean urchin abundance ± 95% CI") +
theme_classic(base_size = 18) +
theme(axis.title.x = element_blank(),
legend.position = c(.1, .9))
# plot without legend #
urchin_plot <- ggplot(data = plot_data_Site4, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_bar(position = "dodge", stat="identity", width = .8) +
scale_x_discrete(limits = position) +
scale_y_continuous(breaks = seq(0, 30, 5)) +
geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = "Mean urchin abundance ± 95% CI") +
theme_classic(base_size = 18) +
theme(axis.title.x = element_blank(),
legend.position = "none")
# combined plot #
mult_compare4 <- c( "A", "A","B", "C")
urchin_plot_final <- ggplot(data = plot_data_Site4, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_bar(position = "dodge", stat="identity", width = .8) +
scale_x_discrete(limits = position) +
scale_y_continuous(breaks = seq(0, 30, 5)) +
geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
geom_text(aes(label = mult_compare4, y = plot_data_Site4$upper + 0.05), vjust = -.5, position = position_dodge(width = 0.8)) +
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = "Mean urchin abundance ± 95% CI") +
theme_classic(base_size = 13) +
theme(axis.title.x = element_blank(),
legend.position = "none",
axis.text.y = element_text(angle = 90))
urchin_plot_final
## Urchin Time Series Plots
### Summary Databases
## Time Series Plots ###
## Subsetting for time series figures ##
HAN_OPE_urchin <- mean_urchin_totals2 %>% filter(Site_long == "Hanauma Bay", Treatment_long == "Open")
HAN_CLO_urchin <- mean_urchin_totals2 %>% filter(Site_long == "Hanauma Bay", Treatment_long == "Closed")
WAI_OPE_urchin <- mean_urchin_totals2 %>% filter(Site_long == "Waikiki", Treatment_long == "Open")
WAI_CLO_urchin <- mean_urchin_totals2 %>% filter(Site_long == "Waikiki", Treatment_long == "Closed")
### Population SE ###
std.dev.pop <- function(x) sqrt(sum((x - mean(x))^2)/(length(x)))
std.error.pop <- function(x) (std.dev.pop(x))/(sqrt(length(x)))
plot_data_urchin <- mean_urchin_totals2 %>%
group_by(TimeStep, Shelter, Treatment_long, Site_long) %>%
summarise(mean = mean(mean_urchin_abundance),
sd = std.dev.pop(mean_urchin_abundance),
lower = mean(mean_urchin_abundance) - std.error.pop(mean_urchin_abundance),
upper = mean(mean_urchin_abundance) + std.error.pop(mean_urchin_abundance),
Date = mean(new_date))
# add shelter column #
plot_data_urchin$Shelter <- ifelse(plot_data_urchin$Treatment_long == "Closed", "Low", "High")
plot_data_urchin$Shelter <- factor(plot_data_urchin$Shelter, levels = c("Low", "High"))
### Time Series Plots
## full plot ##
urchin_time_series_plot <- ggplot(data = plot_data_urchin, aes(x = Date, y = mean, group = interaction(Site_long, Treatment_long))) + geom_line(aes(linetype = Treatment_long)) +
geom_point(aes(shape = Site_long, size = 3, color = Treatment_long)) +
guides(size = FALSE) +
theme(text = element_text(size = 15)) +
geom_errorbar(aes(ymin = lower, ymax = upper)) +
scale_x_date(date_breaks = "1 month", date_labels = "%b%y") +
scale_y_continuous(breaks=seq(0, 10, .5)) +
labs(x = "Date", y = "Mean urchin abundance ± SEM")
## cropped plot ##
urchin_time_series_plot <- ggplot(data = plot_data_urchin, aes(x = Date, y = mean, fill = Shelter, shape = Site_long)) +
geom_point(aes(size = 3)) +
geom_line(aes(linetype = Shelter)) +
scale_shape_manual(values = c(21, 24)) +
scale_fill_manual(values = c(NA, "black"), guide = guide_legend(override.aes = list(shape = 21))) +
guides(size = FALSE) +
theme(text = element_text(size = 15)) +
geom_errorbar(aes(ymin = lower, ymax = upper), width = 0) +
scale_x_date(date_breaks = "1 month", date_labels = "%b") +
scale_y_continuous(breaks=seq(0, 20, 2)) +
coord_cartesian(ylim = c(0, 18)) +
labs(x = "Date", y = "Mean urchin abundance ± SEM") +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), axis.title.x = element_blank(), axis.title = element_text(size = rel(1.5)), axis.text = element_text(size = rel(1.5)), legend.text = element_text(size = rel(1.5)), legend.title = element_text(size = rel(1.5)))
## cropped plot for combined figure ##
legend_image <- readPNG('Legend_final2.png')
xmin <- as.Date("2018/01/01")
xmax <- as.Date("2018/03/21")
ymin <- 40
ymax <- 51
urchin_time_series_plot <- ggplot(data = plot_data_urchin, aes(x = Date, y = mean, fill = Shelter, shape = Site_long)) +
geom_point(aes(size = 3)) +
geom_line(aes(linetype = Shelter)) +
scale_linetype_manual(values=c("dashed", "solid")) +
scale_shape_manual(values = c(21, 24)) +
scale_fill_manual(values = c(NA, "black"), guide = guide_legend(override.aes = list(shape = 21))) +
guides(size = FALSE) +
theme(text = element_text(size = 1)) +
geom_errorbar(aes(ymin = lower, ymax = upper), width = 0) +
scale_x_date(date_breaks = "1 month", date_labels = "%b") +
scale_y_continuous(breaks=seq(0, 55, 10)) +
coord_cartesian(ylim = c(0, 55)) +
labs(x = "Date", y = "Mean urchin abundance ± SEM") +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), axis.title.x = element_blank(), axis.title = element_text(size = rel(1)), axis.text = element_text(size = rel(1)), legend.text = element_text(size = rel(1.5)), axis.text.y = element_text(angle = 90), legend.title = element_text(size = rel(1.5)), legend.position = "none")
urchin_time_series_plot
I am looking to make an output where the html created has the urchin figure the same as the first figure from above. Thank you very much for your time!
Related
Customize x axis tick in a bar graph with a factor different than the x factor in aes command
I am making this graph with x = Rot.Herb and y = kg.ha. I want to label the ticks on the x axis with Rot instead of Rot.Herb. Below is the code that I used, learning from this problem Overflowing X axis (ggplot2) ggplot(grp1, aes(x=Rot.Herb, y=value, fill=factor(Herb.trt)))+ geom_bar(stat="identity", position="dodge", width=1)+ scale_x_discrete(expand=c(0.2,0), labels = as.character(Rot))+ scale_fill_manual(values=c("#EEAD0E","#BB0000"), labels=c("Conventional","Low"))+ theme_bw() + theme(panel.grid.major=element_blank()) + facet_grid(~Crop, scales = "free_x", space="free_x")+ theme(legend.title=element_blank(),legend.text=element_text(size=15),legend.position=c(.3,.8))+ geom_errorbar(aes(ymin=value-se, ymax=value+se), size=0.75, width=.25,position=position_dodge(.5))+ xlab("Rotation") + theme(axis.title = element_text(size=24,face="bold", vjust=4), axis.text.x = element_text(size=20, color="black")) + ylim(c(0,1500))+ ylab("Total weed biomass (Kg/ha)\n") + theme(axis.title = element_text(size=24,face="bold", vjust=2), axis.text.y = element_text(size=20, color="black"))+ theme(strip.text.x = element_text(colour = "black", size = 15), strip.background = element_rect(fill = "white")) and R returns an error as I say scale_x_discrete(expand=c(0.2,0), labels = as.character(Rot)) The error is Error in check_breaks_labels(breaks, labels) : object 'Rot' not found Here is my data > dput(grp1) structure(list(Rot = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2-year", "3-year", "4-year"), class = "factor"), Rot.Herb = structure(c(3L, 4L, 13L, 14L, 5L, 6L, 9L, 10L, 15L, 16L, 1L, 2L, 7L, 8L, 11L, 12L, 17L, 18L), .Label = c("A4-conv", "A4-low", "C2-conv", "C2-low", "C3-conv", "C3-low", "C4-conv", "C4-low", "O3-conv", "O3-low", "O4-conv", "O4-low", "S2-conv", "S2-low", "S3-conv", "S3-low", "S4-conv", "S4-low"), class = "factor"), Rot.trt = structure(c(2L, 2L, 7L, 7L, 3L, 3L, 5L, 5L, 8L, 8L, 1L, 1L, 4L, 4L, 6L, 6L, 9L, 9L), .Label = c("A4", "C2", "C3", "C4", "O3", "O4", "S2", "S3", "S4"), class = "factor"), Crop = structure(c(2L, 2L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("alfalfa", "corn", "oat", "soybean"), class = "factor"), Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L), Herb.trt = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("conv", "low"), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "kg.ha", class = "factor"), N = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), value = c(0.43168573275, 16.22463846275, 0.554446363, 25.89844593075, 13.882743364, 84.1922080725, 84.2625, 105.7375, 0.8350420895, 106.7464386085, 127.8875, 65.3875, 1.964170084, 29.46524929925, 249.7625, 333.125, 0.535560112, 28.757014893), sd = c(0.528135488290589, 20.3216237207314, 0.8810357408252, 40.4576156325903, 27.3195114207629, 71.6424882235524, 22.6948736869511, 40.9587266851237, 0.931926987921512, 108.45555847826, 89.1035387905553, 25.141843442092, 2.20017281317418, 24.1072626208021, 93.2983509589889, 85.0439543216722, 0.48339034731648, 33.2628789566726), se = c(0.264067744145294, 10.1608118603657, 0.4405178704126, 20.2288078162951, 13.6597557103815, 35.8212441117762, 11.3474368434756, 20.4793633425619, 0.465963493960756, 54.2277792391299, 44.5517693952777, 12.570921721046, 1.10008640658709, 12.0536313104011, 46.6491754794945, 42.5219771608361, 0.24169517365824, 16.6314394783363), ci = c(0.840381416699796, 32.3362381637038, 1.40192446910603, 64.3770946952626, 43.4714390915815, 113.999185974188, 36.112608456959, 65.1744742040986, 1.48290379975249, 172.576995683309, 141.783613905853, 40.0062833851536, 3.50096592013592, 38.3600344290379, 148.458496149248, 135.323909108861, 0.76918191241357, 52.928663119381 )), .Names = c("Rot", "Rot.Herb", "Rot.trt", "Crop", "Year", "Herb.trt", "variable", "N", "value", "sd", "se", "ci"), row.names = c(NA, -18L), class = "data.frame") and my graph The graph that I want should say "4-year" at the tick in between the two bars in the Alfalfa grid.
Remove space between bars within a grid
I want to move bars that are within one grid closer to save space. Is there a way to do that? The code I used just change the bar width and does not change the spacing. I am aware that bindwidth no longer work in geom_bar as I referred to these problems: Adding space between bars in ggplot2 Increase space between bars in ggplot This is the data dput(grp1) structure(list(Rot = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2-year", "3-year", "4-year"), class = "factor"), Rot.Herb = structure(c(3L, 4L, 13L, 14L, 5L, 6L, 9L, 10L, 15L, 16L, 1L, 2L, 7L, 8L, 11L, 12L, 17L, 18L), .Label = c("A4-conv", "A4-low", "C2-conv", "C2-low", "C3-conv", "C3-low", "C4-conv", "C4-low", "O3-conv", "O3-low", "O4-conv", "O4-low", "S2-conv", "S2-low", "S3-conv", "S3-low", "S4-conv", "S4-low"), class = "factor"), Rot.trt = structure(c(2L, 2L, 7L, 7L, 3L, 3L, 5L, 5L, 8L, 8L, 1L, 1L, 4L, 4L, 6L, 6L, 9L, 9L), .Label = c("A4", "C2", "C3", "C4", "O3", "O4", "S2", "S3", "S4"), class = "factor"), Crop = structure(c(2L, 2L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("alfalfa", "corn", "oat", "soybean"), class = "factor"), Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L), Herb.trt = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("conv", "low"), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "kg.ha", class = "factor"), N = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), value = c(0.43168573275, 16.22463846275, 0.554446363, 25.89844593075, 13.882743364, 84.1922080725, 84.2625, 105.7375, 0.8350420895, 106.7464386085, 127.8875, 65.3875, 1.964170084, 29.46524929925, 249.7625, 333.125, 0.535560112, 28.757014893), sd = c(0.528135488290589, 20.3216237207314, 0.8810357408252, 40.4576156325903, 27.3195114207629, 71.6424882235524, 22.6948736869511, 40.9587266851237, 0.931926987921512, 108.45555847826, 89.1035387905553, 25.141843442092, 2.20017281317418, 24.1072626208021, 93.2983509589889, 85.0439543216722, 0.48339034731648, 33.2628789566726), se = c(0.264067744145294, 10.1608118603657, 0.4405178704126, 20.2288078162951, 13.6597557103815, 35.8212441117762, 11.3474368434756, 20.4793633425619, 0.465963493960756, 54.2277792391299, 44.5517693952777, 12.570921721046, 1.10008640658709, 12.0536313104011, 46.6491754794945, 42.5219771608361, 0.24169517365824, 16.6314394783363), ci = c(0.840381416699796, 32.3362381637038, 1.40192446910603, 64.3770946952626, 43.4714390915815, 113.999185974188, 36.112608456959, 65.1744742040986, 1.48290379975249, 172.576995683309, 141.783613905853, 40.0062833851536, 3.50096592013592, 38.3600344290379, 148.458496149248, 135.323909108861, 0.76918191241357, 52.928663119381 )), .Names = c("Rot", "Rot.Herb", "Rot.trt", "Crop", "Year", "Herb.trt", "variable", "N", "value", "sd", "se", "ci"), row.names = c(NA, -18L), class = "data.frame") This is the code ggplot(grp1, aes(x=Rot.Herb, y=value, fill=factor(Herb.trt)))+ geom_bar(stat="identity", position=position_dodge(.), width=.5)+ scale_fill_brewer(palette = "Dark2")+ theme_bw() + theme(panel.grid.major=element_blank()) + facet_grid(~Crop, scales = "free_x", space="free_x")+ theme(legend.title=element_blank(),legend.text=element_text(size=20),legend.position="top")+ geom_errorbar(aes(ymin=value-se, ymax=value+se), size=0.75, width=.25,position=position_dodge(.5))+ xlab("Treatment") + theme(axis.title = element_text(size=24,face="bold", vjust=4)) + ylab("2014 total weed biomass (Kg/ha)\n") + theme(axis.title = element_text(size=24,face="bold", vjust=2), axis.text.y = element_text(size=20, color="black"))+ theme(strip.text.x = element_text(colour = "black", size = 15), strip.background = element_rect(fill = "white"), axis.text.x = element_blank(), axis.ticks.x = element_blank()) And graph What I want is a graph with bar width of 0.5 and no spacing between bars in the same grid. Thank you for all the input.
I think you want width = 0.5 while keeping the bars touching because you don't want the absolute width to change (width = 0.5 changes the relative width). This might be closer to what you are asking for. ggplot(grp1, aes(x=Rot.Herb, y=value, fill=factor(Herb.trt)))+ geom_bar(stat="identity", position = "dodge", width=1)+ scale_x_discrete(expand=c(0.2,0))+ scale_fill_brewer(palette = "Dark2")+ theme_bw() + theme(panel.grid.major=element_blank()) + facet_grid(~Crop, scales = "free_x", space="free_x")+ theme(legend.title=element_blank(),legend.text=element_text(size=20),legend.position="top")+ geom_errorbar(aes(ymin=value-se, ymax=value+se), size=0.75, width=.25,position=position_dodge(0.5))+ xlab("Treatment") + theme(axis.title = element_text(size=24,face="bold", vjust=4)) + ylab("2014 total weed biomass (Kg/ha)\n") + theme(axis.title = element_text(size=24,face="bold", vjust=2), axis.text.y = element_text(size=20, color="black"))+ theme(strip.text.x = element_text(colour = "black", size = 15), strip.background = element_rect(fill = "white"), axis.text.x = element_blank(), axis.ticks.x = element_blank()) Basically I changed the width = 1 as suggested by #Gregor, which causes the bars to touch, but added an extra option scale_x_discrete(expand=c(0.2,0)) to add spaces between the bars and the sides of the plot. This in effect adjusts the absolute width of the bars while keeping them touching. The first number gives the multiplicative constant used to expand the range, while the second gives the additive. You can experiment with the two values in expand = to get what you want. Also see this related question: ggplot geom_boxplot: reduce space between x-axis categories
Plot values in ggplot geom_lines
I have a dataframe like this one: > dput(df) structure(list(OBBLIGATORIO = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("no", "yes"), class = "factor"), COUNTRY = structure(c(16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), .Label = c("Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom of Great Britain and Northern Ireland" ), class = "factor"), YEAR = c(2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L), AGE = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Total", class = "factor"), `CAUSE OF DEATH` = c("Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough" ), VALUE = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 2L, 2L, 2L, 1L, 1L, 6L, 7L, 7L, 1L, 2L, 3L, 2L, 5L, 12L, 9L, 13L, 9L, 13L, 8L, 17L, 14L, 16L, 18L, 15L, 19L, 11L, 10L, 25L, 24L, 21L, 22L, 23L, 20L, 34L, 32L, 31L, 30L, 29L, 28L, 27L, 26L, 41L, 42L, 43L, 45L, 46L, 47L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 44L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 1L), .Label = c("0", "1", "2", "3", "6", "7", "9", "17", "18", "19", "21", "22", "27", "28", "30", "31", "37", "41", "42", "301", "329", "333", "344", "350", "396", "413", "415", "460", "517", "558", "597", "609", "622", "647", "681", "1087", "1349", "1413", "1448", "1499", "1576", "1654", "1725", "1948", "2531", "2665", "2757" ), class = "factor"), ID = 1:98), .Names = c("OBBLIGATORIO", "COUNTRY", "YEAR", "AGE", "CAUSE OF DEATH", "VALUE", "ID"), row.names = c(NA, -98L), class = "data.frame") I want to obtain a chart that: on x axis there are values from YEAR column on y axis there are values from VALUE column data are divided by CAUSE OF DEATH column So something like: I try: x11() ggplot(df, aes(x = df$`YEAR`, y = df$`VALUE`, fill = df$`CAUSE OF DEATH`, colour = df$`CAUSE OF DEATH`)) + geom_density(alpha = 0.1) + xlim(1995, 2010) But the result is completely different from the one I want. Thanks
I'm not sure what your actual question is, but one problem with your dataframe is that the VALUE column is currently defined as a factor, not as as a numeric. I think that remedying this will go a long way to solving your problem. I do this post-facto below (i.e. after the dataframe is already created), but if you are getting the data into R via a read.table() or similar command, you can specify the class of your columns at data frame creation time, which is probably a better approach. In my code below I use the dplyr package for manipulating dataframes. It's quite powerful, but for this particular example it isn't doing anything that base R couldn't do. require(ggplot2) require(dplyr) require(magrittr) df <- ### YOUR dput output goes here ### # fix the problem with the `VALUE` column df %<>% mutate(VALUE = VALUE %>% as.character %>% as.numeric) # equivalent in base R: # df$VALUE <- as.numeric(as.character(df$VALUE)) # make a graph (is it the one you want?) df %>% group_by(YEAR, `CAUSE OF DEATH`) %>% summarize(value = sum(VALUE)) %>% ggplot(aes(x = YEAR, y = value, color = `CAUSE OF DEATH`)) + geom_line() + theme_bw() + geom_point() # save graph for uploading to SO ggsave('SO37230266.png') The result is this graph:
1 bar in wrong order in stacked bar chart (R ggplot2)
I made a stacked bar chart, however the segments of the 3rd bar are in a different order; the segment that should be on the bottom is now on the top, see image: When I change the order of the x axis, it does exactly the same with the 3rd bar. How can I solve this? Something wrong with the code? library('ggplot2') bar <- ggplot(data.location, aes(Location, value, fill=variable)) bar + stat_summary(fun.y=mean, geom="bar", position="stack")+labs(x="Location", y="value", fill="variable") Added: output from dput(data.location) dput(data.location[data.location$Location %in% c('BRM', 'CG', 'DDO'),]) structure(list(Location = structure(c(2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L), .Label = c("BA", "BRM", "CG", "CH", "DDO", "DR", "FB", "GG", "GI", "GQS", "HC", "HS", "LL1", "LL2", "MOW", "PP", "TP", "TR", "TRD", "WB"), class = "factor"), Zone = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), variable = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("A", "B", "C", "D" ), class = "factor"), value = c(425.810194245673, 815.265455416096, 735.274721619422, 997.041922511793, 2147.03610300279, 1210.08829970945, 0, 177.692085889937, 173.266014794846, 406.498315338813, 1293.35105648741, 234.022025228589)), .Names = c("Location", "Zone", "variable", "value"), row.names = c(2L, 3L, 5L, 22L, 23L, 25L, 42L, 43L, 45L, 62L, 63L, 65L), class = "data.frame")
Using the following code, I get consistent order of the bars. ggplot(data.location,aes(Location,value,fill=variable,order=variable))+ geom_bar(stat="identity")
How to center values in stackbar plot and add greek text to legend with ggplot
I have created a stacked-bar plot with 'ggplot' to display my karyotype (molecular) results from a transplant experiment, with each panel representing a location, and the x-axis is the various substrates, while the y-axis is the percentage of each of the three karyotypes. I have looked over several examples of questions and answers from Stack Overflow and cannot figure out how to do the following: centre the values (should be rounded to two decimal places) within each section of the stacked bars, right now I just have them offset from the top. how to change my legend text from "BB" to the Greek "lower alpha, lower alpha", "BD" to Greek "lower alpha, lower beta", and "DD" to Greek "lower beta, lower beta". Here is some sample data and code with a copy of the plot it generates. Karotype.Data <- structure(list(Location = structure(c(1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 1L, 4L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Kampinge", "Kaseberga", "Molle", "Steninge"), class = "factor"), Substrate = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 4L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 1L, 4L, 2L), .Label = c("Kampinge", "Kaseberga", "Molle", "Steninge"), class = "factor"), Karyotype = structure(c(1L, 3L, 4L, 1L, 3L, 3L, 4L, 4L, 4L, 3L, 1L, 4L, 3L, 4L, 2L, 3L, 1L, 4L, 3L, 2L, 4L, 3L, 4L, 2L, 3L), .Label = c("", "BB", "BD", "DD"), class = "factor")), .Names = c("Location", "Substrate", "Karyotype"), row.names = c(135L, 136L, 137L, 138L, 139L, 165L, 166L, 167L, 168L, 169L, 236L, 237L, 238L, 239L, 240L, 326L, 327L, 328L, 329L, 330L, 426L, 427L, 428L, 429L, 430L), class = "data.frame") z.counts <- Karotype.Data %>% group_by(Location,Substrate,Karyotype) %>% summarise(Frequency=n()) z.freq <- z.counts %>% filter(Karyotype != '') %>% group_by(Location,Substrate) %>% mutate(Percent=Frequency/sum(Frequency)) z.freq ggplot(z.freq, aes(x=Substrate, y=Percent, fill=Karyotype )) + geom_bar(stat="identity") + geom_text(aes(label = Percent), size = 5, vjust = 1, position = "stack") + facet_wrap(~ Location, ncol=2) + scale_y_continuous(name="Percentage") + theme(strip.text.x = element_text(colour="black", size=20, face="bold"), axis.title.x = element_text(colour="black", size=20, face="bold", vjust=-0.5), axis.text.x = element_text(colour="black", size=18), axis.title.y = element_text(colour="black", size=20,face="bold", vjust=1), axis.text.y = element_text(colour="black", size=18), legend.title = element_text(colour="black", size=20, face="bold"), legend.text = element_text(colour="black", size = 18), legend.position="bottom")
To add greek letters to the legend, you can change the colour scale with scale_colour_manual(): test = data.frame(x=1:30,y=1:30,label=rep(c("BB","BD","DD"),each=10)) ggplot(test) + geom_point(aes(x=x,y=y,color=label)) + scale_colour_manual(values=c(1,2,3),breaks = c("BB","BD","DD"),labels = list(bquote(alpha~alpha),bquote(alpha~beta),bquote(beta~beta))) argument values sets the colour, breaks sets your breakpoints (BB, BD and DD) and labels sets the greek letters you want. To round the legend, you can add another column to your dataframe, setting values to round(Percent,digits=3), and use this column in the geom_text. Informations concerning greek letters in ggplot2 can be found here