Define seperate aesthetics for geom_smooth when grouping data - r

I have some batch test data which was performed under different mixing conditions.
While I have managed to group the data, I am not sure how to define different aethitics for the geom_smooth lines.
M3<- ggplot(subset(VFA2, VFA %in% "HPr"), aes(x = Time, y = value, group = mix, shape = Test, colour = Test))+geom_point(size = 6, colour = "purple4")+
labs(x = "Anaerobic Time (h)\n Continuous Mixing", y = "HPr Concentration\n(mg HPr/L)")+theme(panel.background = element_rect(fill = "White", colour = "grey"),panel.grid.major = element_line(color = 'grey'), legend.position="bottom", text= element_text(size = 28, family = "Arial"))+
scale_x_discrete(breaks = factor(VFA2$Time), expand = c(-0.25,2))+scale_y_continuous(limits = c(-0.25, 60),breaks = c(0, 10,20, 30, 40, 50, 60))+[![enter image description here][1]][1] scale_shape_manual(values = c(0,1,2,5,7,13,14,9))
M3<- M3 + geom_smooth(method= "gam", formula = y~poly(x,4), se = F, colour = "black", linetype = "dashed")

Is this what you are looking for :
M3<- ggplot(subset(VFA2, VFA %in% "HPr"), aes(x = Time, y = value, group = mix, shape = Test))+
geom_point(size = 6, colour = "purple4")+
geom_smooth(aes(colour=mix), method= "gam", formula = y~poly(x,4), se = F, linetype = "dashed")+
labs(x = "Anaerobic Time (h)\n Continuous Mixing", y = "HPr Concentration\n(mg HPr/L)")+
theme(panel.background = element_rect(fill = "White", colour = "grey"),panel.grid.major = element_line(color = 'grey'), legend.position="bottom", text= element_text(size = 28, family = "Arial"))+
scale_x_discrete(breaks = factor(VFA2$Time), expand = c(-0.25,2))+
scale_y_continuous(limits = c(-0.25, 60),breaks = c(0, 10,20, 30, 40, 50, 60))+
scale_shape_manual(values = c(0,1,2,5,7,13,14,9))+
scale_color_manual(values = c("black", "red"))

Related

How to modify the x axis levels without affecting the number of points of the plot?

Need to display the x-axis levels in neatly way without affecting the actual point numbers in the final output. As currently, I am getting x-axis in closely spaced which looks not good while I am showing in powerpoint
library("readxl")
my_data <-read_excel("central_high.xlsx") # Input file
str(my_data)
my_data = as.data.frame(my_data)
str(my_data)
my_data$var1 = NULL
f20 = as.data.frame(table(my_data$Year20))
f20$Var1 = as.Date(f20$Var1, "%Y-%m-%d")
f20$Var1 = format(f20$Var1, format="%m-%d")
f20$Cumulative_F20 = cumsum(f20$Freq) # cumulative calculation
f20
newcol_20 = c( my_data$Year19,
my_data$Year18, my_data$Year17,
my_data$Year16, my_data$Year15,
my_data$Year14, my_data$Year13,
my_data$Year12, my_data$Year11,
my_data$Year10, my_data$Year9,
my_data$Year8, my_data$Year7,
my_data$Year6, my_data$Year5,
my_data$Year4, my_data$Year3,
my_data$Year2, my_data$Year1)
str(newcol_20)
newdata_20 = data.frame(newcol_20)
str(newdata_20)
newdata_20$newcol_20 = as.Date(as.character(newdata_20$newcol_20), "%Y-%m-%d")
newdata_20$newcol_20 = format(newdata_20$newcol_20, format="%m-%d")
str(newdata_20)
newtable_20 = table(newdata_20$newcol_20)
newtable_20
newdf_20 = as.data.frame(newtable_20)
#newdf_20$Cumulative_20 = cumsum(newdf_20$Freq)/19 # cumulative calculation
newdf_20$Freq = newdf_20$Freq/19
newdf_20
newcol_05 = c( my_data$Year19,
my_data$Year18, my_data$Year17,
my_data$Year16)
str(newcol_05)
newdata_05 = data.frame(newcol_05)
str(newdata_05)
newdata_05$newcol_05 = as.Date(as.character(newdata_05$newcol_05), "%Y-%m-%d")
newdata_05$newcol_05 = format(newdata_05$newcol_05, format="%m-%d")
str(newdata_05)
newtable_05 = table(newdata_05$newcol_05)
newtable_05
newdf_05 = as.data.frame(newtable_05)
newdf_05$Cumulative_05 = cumsum(newdf_05$Freq)/4 # cumulative calculation
newdf_05$Freq = newdf_05$Freq/4
newdf_05
library(ggplot2)
library(ggpubr)
ggplot() +
geom_line(data = newdf_20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#111111"), size = 1.6) +
geom_line(data = newdf_05, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#999999"), size = 1.6) +
geom_line(data = f20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#CC79A7"), size = 1.6) +
geom_vline(xintercept = "03-25", color="gray", size=1)+
geom_vline(xintercept = "04-21", color="gray", size=1)+
labs(y = "Cumulative_Frequency", colour= "#000000", size = 16 )+
font("ylab", size = 15, color = "black", face = "bold.italic")+
font("legend.text",size = 10, face = "bold")+
font("legend.title",size = 15, face = "bold")+
theme(axis.line.x = element_line(size = 0.5, colour = "black"), # theme modification
axis.line.y = element_line(size = 0.5, colour = "black"),
#axis.text.x=element_blank(),axis.ticks.x=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.text.x = element_text(colour = "#000000", size = 7,
angle = 90, face ="bold" ),
axis.text.y = element_text(colour = "#000000", size = 12,
angle = 90, face ="bold" ))
Please modify the code and I also added the final output what I am getting need a little bit of modification in the code to get x-axis neatly
One option would be dodging the labels in x-axis:
library(ggplot2)
library(ggpubr)
ggplot() +
geom_line(data = newdf_20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#111111"), size = 1.6) +
geom_line(data = newdf_05, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#999999"), size = 1.6) +
geom_line(data = f20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#CC79A7"), size = 1.6) +
geom_vline(xintercept = "03-25", color="gray", size=1)+
geom_vline(xintercept = "04-21", color="gray", size=1)+
scale_x_discrete(guide = guide_axis(n.dodge=2))+
labs(y = "Cumulative_Frequency", colour= "#000000", size = 16 )+
font("ylab", size = 15, color = "black", face = "bold.italic")+
font("legend.text",size = 10, face = "bold")+
font("legend.title",size = 15, face = "bold")+
theme(axis.line.x = element_line(size = 0.5, colour = "black"), # theme modification
axis.line.y = element_line(size = 0.5, colour = "black"),
#axis.text.x=element_blank(),axis.ticks.x=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.text.x = element_text(colour = "#000000", size = 7,
angle = 90, face ="bold" ),
axis.text.y = element_text(colour = "#000000", size = 12,
angle = 90, face ="bold" ))
Output:

how to use facet_wrap to ggplot multiple location point in R?

I am trying to plot multiple location data using facet_wrap functionality of ggplot. I am having trouble in creating the legends (95% confidence interval is completely missed). Below is my code and I would appreciate any suggestion.
library(ggplot2)
library(lubridate)
set.seed(123)
DF1 <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "1 month"),
Ob = runif(60,1,5), L95 =runif(60, 0,4), U95 = runif(60,2,7), Sim = runif(60,1,5),
Loc = rep("Upstream", 60))
DF2 <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "1 month"),
Ob = runif(60,1,5), L95 =runif(60, 0,4), U95 = runif(60,2,7), Sim = runif(60,1,5),
Loc = rep("Downstream", 60))
DF <- dplyr::bind_rows(DF1,DF2)
DF$Loc <- factor(DF$Loc, levels = c("Upstream","Downstream"))
ggplot(DF, aes(x = Date))+
geom_ribbon(aes(ymin = L95, ymax = U95), fill = "grey30", alpha = 0.4)+
geom_line(aes(y = Ob, color = "blue"), size = 1 )+
geom_line(aes(y = Sim, color = "black"), size = 1, linetype = "dashed")+
geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
facet_wrap(~ Loc, ncol = 1, scales = "free_y")+
theme_bw()+
scale_color_identity(guide = "legend", breaks = c("grey30", "blue", "black"),
labels = c("95% confidence bound", "Observation","Simulation"))
Your fill is outside the aes function, so it cannot appear in the legend.
ggplot(DF, aes(x = Date))+
geom_ribbon(aes(ymin = L95, ymax = U95, color = "grey30"), fill = "grey30", alpha = 0.4)+
geom_line(aes(y = Ob, color = "blue"), size = 1 )+
geom_line(aes(y = Sim, color = "black"), size = 1, linetype = "dashed")+
geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
facet_wrap(~ Loc, ncol = 1, scales = "free_y")+
theme_bw()+
scale_color_identity(guide = "legend", breaks = c("grey30", "blue", "black"),
labels = c("95% confidence bound", "Observation","Simulation"))
ggplot(DF, aes(x = Date))+
geom_ribbon(aes(ymin = L95, ymax = U95, fill = "grey30"), alpha = 0.4)+
geom_line(aes(y = Ob, color = "blue"), size = 1 )+
geom_line(aes(y = Sim, color = "black"), size = 1, linetype = "dashed")+
geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
facet_wrap(~ Loc, ncol = 1, scales = "free_y")+
theme_bw()+
scale_color_identity(guide = "legend", breaks = c( "blue", "black"),
labels = c( "Observation","Simulation"),
name = 'Legend')+
scale_fill_identity(guide = "legend", labels = c("95% confidence bound"),
name=NULL)+
theme(legend.spacing.y = unit(-0.2, "cm"))+
theme(legend.title = element_text(margin=margin(b = 0.4, unit='cm')))
Here is how I would do this.
First, you are using fill for the ribbon, not color. Second, you need to actually map fill in aes, not just set it outside the aes. Then I would give the names you'd like in the aes calls, and use scale_*_manual to set the values you'd like:
ggplot(DF, aes(x = Date))+
geom_ribbon(aes(ymin = L95, ymax = U95, fill = "95% confidence bound"), alpha = 0.4)+
geom_line(aes(y = Ob, color = "Observation"), size = 1 )+
geom_line(aes(y = Sim, color = "Simulation"), size = 1, linetype = "dashed")+
geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
facet_wrap(~ Loc, ncol = 1, scales = "free_y")+
theme_bw()+
scale_color_manual(values = c('blue', 'black'), name = NULL) +
scale_fill_manual(values = 'grey30', name = NULL)
There are a number of valid ways to approach this, but this is how many people do it.

About ggplot2: Rotate geom_point shape & Show geom_text above the line

Good Morning.
I am trying to plot using a ggplot2 package but facing a problem below:
To make it more understandable, here is the target image that I want to make.
Just like the image, I want to do the followings:
1) Put a text 'median' above the dashed line so that it is ok to see the character clearly.
2) Rotate the degree of triangle (Not ^ ^ but < >) so that it makes sense.
To achieve the above, I've done so far with the codes:
# binding the data, defining the x and y aesthetics, title, labels
w_plot <- ggplot(
data = com_mal,
aes(x = reorder(name, -median_age), y = median_age)
)
labels = c('5 yrs old', 10, 15, 20, 25, 30)
w_plot +
geom_linerange(
aes(ymin = q1_age, ymax = q3_age),
color = "#76bde0",
size = 6,
alpha = 0.7
) +
geom_point(fill = "#ed3324", colour = "white", size = 4, shape = 21) +
geom_text(aes(y = 9, x = 15, label = '25th')) +
geom_text(aes(y = 20, x = 15, label = '75th percentile')) +
geom_text(aes(y = 30, x = 22, label = 'median')) +
geom_point(aes(y = 8.25, x = 15), shape = 17) +
geom_point(aes(y = 21.75, x = 15), shape = 17) +
geom_point(aes(y = 29, x = 21.9), fill = "#ed3324", colour = "white", size = 4, shape = 21) +
geom_hline(aes(yintercept = 5), linetype = 'dotted') +
geom_hline(aes(yintercept = 10), linetype = 'dotted') +
geom_hline(aes(yintercept = 15), linetype = 'dotted') +
geom_hline(aes(yintercept = 20), linetype = 'dotted') +
geom_hline(aes(yintercept = 25), linetype = 'dotted') +
geom_hline(aes(yintercept = 30), linetype = 'dotted') +
scale_y_continuous(breaks = seq(5, 30, by = 5), position = 'right', labels = labels) +
coord_flip() +
labs(title = 'Youngest Male Names',
subtitle = 'By estimated median age for Americans alive as of Jan 1. 2014',
x = NULL, y = NULL, caption = 'SOURCE: SOCIAL SECURITY ADMINISTRATION') +
theme(plot.title = element_text(face = 'bold', size = 16),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.ticks = element_blank(), plot.caption = element_text(size = 10))
Thank you very much!
For the triangles, you could use geom_text() instead, setting the family argument to a font that supports the character, and for the label use geom_label():
geom_text(label = "▶", size = 3, family = "HiraKakuPro-W3")
geom_label(aes(y = 4, x = 10, label = 'median'), fill = "grey92", label.size = NA)
label.size removes the outline of the label, and "grey92" is (approximately?) the color of the background.
If you want the dotted line to be behind the label, you should add geom_label() to the plot after the line. (Also notice that you can add all the dotted lines in the same line of code.)
w_plot +
geom_linerange(
aes(ymin = q1_age, ymax = q3_age),
color = "#76bde0",
size = 6,
alpha = 0.7
) +
geom_point(fill = "#ed3324", colour = "white", size = 4, shape = 21) +
geom_text(aes(y = 9, x = 15, label = '25th')) +
geom_text(aes(y = 20, x = 15, label = '75th percentile')) +
geom_text(aes(y = 8.25, x = 15),label = "◀", size = 3,
family = "HiraKakuPro-W3") +
geom_text(aes(y = 21.75, x = 15),label = "▶", size = 3,
family = "HiraKakuPro-W3") +
geom_point(aes(y = 29, x = 21.9), fill = "#ed3324", colour = "white",
size = 4, shape = 21) +
geom_hline(yintercept = seq(5, 30, by = 5), linetype = 'dotted') +
geom_label(aes(y = 30, x = 22, label = 'median'),
fill = "grey92", label.size = NA) +
scale_y_continuous(breaks = seq(5, 30, by = 5),
position = 'right', labels = labels) +
coord_flip() +
labs(title = 'Youngest Male Names',
subtitle = 'By estimated median age for Americans alive as of Jan 1. 2014',
caption = 'SOURCE: SOCIAL SECURITY ADMINISTRATION') +
theme(plot.title = element_text(face = 'bold', size = 16),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.ticks = element_blank(), plot.caption = element_text(size = 10))

Using ggplot2; How to get position_dodge() to work with this example?

Background
I took the data from a Stephen Few Example and wanted to add labels to each of the bars to pull the legend from the side of the graphic.
The code in the "Hack Graphic" section got me there because I couldn't get the position_dodge() to work with the text labels.
Load Data
library(tidyverse)
library(forcats)
### Build data from his table
candidates <- tibble::tibble(`Rating Areas` = c("Experience",
"Communication", "Friendliness", "Subject matter knowledge", "Presentation",
"Education"), `Karen Fortou` = c(4,3.5, 4, 4, 3, 3.5), `Mike Rafun` = c(4.5,
2, 2, 5, 1.5, 4.5), `Jack Nymbul` = c(2.5, 5, 4.5, 2.5, 2.75, 2)) %>%
gather("Candidates", "Score", -`Rating Areas`)
# The totals for each candidate
totals <- candidates %>% group_by(Candidates) %>% summarise(Score =
sum(Score))
Hack Graphic
Notice how I used manually created x-axis values (x = c(seq(.6,1.35, by = .15), seq(1.6,2.35, by = .15), seq(2.6,3.35, by = .15))) to place the labels instead of using position = position_dodge() as described in this post.
candidates %>%
ggplot(aes(x = fct_reorder(Candidates, Score), y = Score)) +
geom_col(data = totals, alpha = .45) +
geom_col(aes(fill = `Rating Areas`), position = position_dodge(.9), color = "black",
show.legend = FALSE) +
geom_text(label = rep(c("Experience", "Communication", "Friendliness",
"Subject matter knowledge", "Presentation", "Education"),3),
x = c(seq(.6,1.35, by = .15), seq(1.6,2.35, by = .15),
seq(2.6,3.35, by = .15)), y = 5.1, angle = 90, color = "black",
hjust = "left", size = 4, fontface = "bold") +
scale_fill_brewer(type = "qual") +
scale_y_continuous(breaks = seq(0, 25, by = 2)) +
theme_bw() +
labs(x = "\nCandidates", y = "Rating Score") +
theme(axis.text.x = element_text(size = 14, color = "black"), legend.text = element_text(size = 14),
legend.title = element_text(size = 15), axis.title = element_text(size = 15))
Graphic Code that doesn't work
When I follow the example from the previous Stack answer using geom_text(aes(label =Rating Areas), position = position_dodge(width = 0.9), angle = 90, color = "black", hjust = "left", size = 4, fontface = "bold") it does not spread the labels out ever each bar.
I must be missing something obvious. Please help with how to get position_dodge() to work with this example?
candidates %>%
ggplot(aes(x = fct_reorder(Candidates, Score), y = Score)) +
geom_col(data = totals, alpha = .45) +
geom_col(aes(fill = `Rating Areas`), position = position_dodge(.9), color = "black", show.legend = FALSE) +
geom_text(aes(label = `Rating Areas`), position = position_dodge(width = 0.9), angle = 90, color = "black", hjust = "left", size = 4, fontface = "bold") +
scale_fill_brewer(type = "qual") +
scale_y_continuous(breaks = seq(0, 25, by = 2)) +
theme_bw() +
labs(x = "\nCandidates", y = "Rating Score") +
theme(axis.text.x = element_text(size = 14, color = "black"), legend.text = element_text(size = 14), legend.title = element_text(size = 15), axis.title = element_text(size = 15))
I think you need to have the same mapping for both geom_col and geom_text. You can add fill = Rating Areas to the aesthetics of geom_text. You will get a warning though.
candidates %>%
ggplot(aes(x = fct_reorder(Candidates, Score), y = Score)) +
geom_col(data = totals, alpha = .45) +
geom_col(aes(fill = `Rating Areas`), position = position_dodge(.9), color = "black", show.legend = FALSE) +
geom_text(aes(fill = `Rating Areas`, label = `Rating Areas`), position = position_dodge(width = 0.9), angle = 90, color = "black", hjust = "left", size = 4, fontface = "bold") +
scale_fill_brewer(type = "qual") +
scale_y_continuous(breaks = seq(0, 25, by = 2)) +
theme_bw() +
labs(x = "\nCandidates", y = "Rating Score") +
theme(axis.text.x = element_text(size = 14, color = "black"), legend.text = element_text(size = 14), legend.title = element_text(size = 15), axis.title = element_text(size = 15))
Edit: Here's a way to do it without the warning:
candidates %>%
ggplot(aes(x = fct_reorder(Candidates, Score), y = Score, fill = `Rating Areas`)) +
geom_col(data = totals, aes(x = fct_reorder(Candidates, Score), y = Score), alpha = .45, inherit.aes = FALSE) +
geom_col(position = position_dodge(.9), color = "black", show.legend = FALSE) +
geom_text(aes(label = `Rating Areas`), position = position_dodge(width = 0.9), angle = 90, color = "black", hjust = "left", size = 4, fontface = "bold") +
scale_fill_brewer(type = "qual") +
scale_y_continuous(breaks = seq(0, 25, by = 2)) +
theme_bw() +
labs(x = "\nCandidates", y = "Rating Score") +
theme(axis.text.x = element_text(size = 14, color = "black"), legend.text = element_text(size = 14), legend.title = element_text(size = 15), axis.title = element_text(size = 15))

Non overlapping axis tick annotations in ggplot2

I have a data.frame df as follows,
df <- data.frame(x = 0:50, y = rnorm(n = 51, mean = 25, sd = 74))
I want to annotate the axis ticks outside the plot in ggplot2. Following this question, I am able to do it as follows,
library(ggplot2)
ggplot(df, aes(x = x, y = y)) +
geom_line() +
geom_vline(xintercept = c(12, 28), colour = "red") +
scale_x_continuous(breaks = c(0,12, 25,28,50), labels = c("0","Point 1", "25","Point 2","50")) +
theme(axis.text.x = element_text(color = c("black","red", "black", "red", "black")),
axis.ticks.x = element_line(color = c("black","red", "black", "red", "black"),
size = c(.5,1,.5,1,.5)))
However, when the points are close, the annotations overlap.
ggplot(df, aes(x = x, y = y)) +
geom_line() +
geom_vline(xintercept = c(30, 28), colour = "red") +
scale_x_continuous(breaks = c(0, 25,28, 30, 50), labels = c("0","25","Point 1", "Point 2","50")) +
theme(axis.text.x = element_text(color = c("black","black", "red", "red", "black")),
axis.ticks.x = element_line(color = c("black","black", "red", "red", "black"),
size = c(.5,.5,1,1,.5)))
How to avoid this and get a desired plot as follows in ggplot2?
What about turning them using angle so they don't overlap.
ggplot(df, aes(x = x, y = y)) +
geom_line() +
geom_vline(xintercept = c(30, 28), colour = "red") +
scale_x_continuous(breaks = c(0, 25,28, 30, 50), labels = c("0","25","Point 1", "Point 2","50")) +
theme(axis.text.x = element_text(color = c("black","black", "red", "red", "black"), angle = 60, hjust = 1),
axis.ticks.x = element_line(color = c("black","black", "red", "red", "black"),
size = c(.5,.5,1,1,.5)))

Resources