I am super new to R. I just started yesterday.
I'm trying to make a scatterplot of absorbance vs wavelength for five different pH conditions. The legend shows up in the order of pH 1, 10, 4, 7, 8, but I want it to be 1, 4, 7, 8, 10. The legend in question I'm not sure how to reorder the legend.
Currently, pH is a character. I tried making it a numeric, which did put the legend in the correct order, but then I cannot choose specific colors for each pH. I don't want a gradient. (To specify, the colors in the image of the legend are the correct ones. only the order of the pH is wrong.)
This is what the graph part of my code looks like.
pH_character %>% #imported data frame
group_by(Wavelength, pH) %>%
summarize(mean_abs = mean(Absorbance), se_abs = sd(Absorbance)/sqrt(3)) %>% #for error bars later
ggplot(aes(Wavelength, mean_abs, color = pH)) +
geom_point(size = 1) +
scale_color_manual(values = c("#ff1934","black","#ffc273","#ff6721","#702400")) + #the colors I need
geom_errorbar(aes(x = Wavelength,
ymin = mean_abs - 2*se_abs,
ymax = mean_abs + 2*se_abs,
width = 5)) +
scale_x_continuous(expand = c(0,0), limits = c( 400,700)) +
scale_y_continuous(expand = c(0,0), limits = c(0,2)) +
labs(x="Wavelength (nm)", y="Absorbance")
I also tried group_by(pH, Wavelength)
instead of group_by(Wavelength, pH)
so that summarize groups the output by pH, but that didn't do anything to the legend order since the output was still organized in the order of pH 1, 10, 4, 7, 8.
Edit: here is code for fake data:
pH_character <- data.frame(Wavelength=c(430, 430, 430, 430, 430, 470, 470, 470, 470, 470, 565, 565, 565, 565, 565, 635, 635, 635, 635, 635),
pH=c("1", "4", "7", "8", "10", "1", "4", "7", "8", "10", "1", "4", "7", "8", "10", "1", "4", "7", "8", "10"),
Absorbance=c(0.96533, 0.83866, 1.268, 1.371, 1.5, 0.78333, 0.617333, 1.192, 1.38566, 1.5, 0.243, 0.12833, 0.22233, 0.26966, 1.5, 0.06566, 0.05, 0.0833, 0.104, 1.5))
This is actually just averaged data except for pH 10 so it won't have error bars (but still same problem)
As I mentioned in my comment, you could get your desired order by converting to a factor
with the order of the levels set in your desired order.
In your case this could be easily achieved using factor(as.numeric(pH))
to convert pH
to a factor
with the order of the levels set according to the numeric value.
Note: You probably have to reorder your color vector too to reflect the changed order of pH
.
library(dplyr)
library(ggplot2)
pH_character %>%
group_by(Wavelength, pH) %>%
summarize(
mean_abs = mean(Absorbance),
se_abs = sd(Absorbance) / sqrt(3),
) %>%
mutate(pH = factor(as.numeric(pH))) %>%
ggplot(aes(Wavelength, mean_abs, color = pH)) +
geom_point(size = 1) +
scale_color_manual(
values = c("#ff1934", "black", "#ffc273", "#ff6721", "#702400")
) +
geom_errorbar(aes(
x = Wavelength,
ymin = mean_abs - 2 * se_abs,
ymax = mean_abs + 2 * se_abs,
width = 5
)) +
scale_x_continuous(
expand = c(0, 0),
limits = c(400, 700)
) +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, 2)
) +
labs(
x = "Wavelength (nm)",
y = "Absorbance"
)