I'm trying to create a simple point estimate with confidence interval plot. I can get it to plot as I'd like until I try to change the point shape and/or the color. When I try to change either I get "Warning: Removed 4 rows containing missing values (geom_point)." and end up with a blank plot.
I've checked out and tried the suggestions on: here here here and here and a couple other places but to no avail.
A Reproducible Example
library(ggplot2)
set.seed(1)
# Create some sample data
point_est <- 4:1
se <- runif(4)
df <- data.frame(point_est = point_est,
se = se,
lower = point_est - se,
upper = point_est + se,
year = c("c", "c", "p", "p"),
group = letters[1:4])
group_names <- paste0("Display Name for \n Group ", LETTERS[1:4])
names(group_names) <- letters[1:4]
legend_text <- c("Previous Year Rate with 95% Confidence Intervals",
"Current Year Rate with 95% Confidence Intervals")
names(legend_text) <- c("p", "c")
df$year = factor(df$year, levels = names(legend_text), labels = legend_text)
df$group = factor(df$group, levels = names(group_names), labels = group_names)
# Plot looks good except the colors and shape of the points need changing
ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
geom_point(size = 3.2) +
scale_x_discrete(drop=FALSE) +
scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
labs(x = NULL,
y = "This is my Left Axis") +
theme(legend.title = element_blank(),
legend.position = "bottom",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA),
panel.background = element_blank())
# now change the shapes of the points and the colors of the error bars
shapes <- c(17, 15)
names(shapes) <- names(legend_text)
colors <- c("pink", "blue")
names(colors) <- names(legend_text)
ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
geom_point(size = 3.2) +
scale_x_discrete(drop=FALSE) +
scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
scale_shape_manual(values = shapes) +
scale_color_manual(values = colors) +
labs(x = NULL,
y = "This is my Left Axis") +
theme(legend.title = element_blank(),
legend.position = "bottom",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA),
panel.background = element_blank())
#> Warning: Removed 4 rows containing missing values (geom_point).
# Blank plot now and warnings:(