For some reason, I can't get the bars and legend color coded and labeled. I'm not sure why. I've used similar code with different data and it works fine. So, I'm not sure if it has something to do with the second y-axis which is new. Thanks for the help!
library(ggplot2)
fulldata <- data.frame(
gender = c("F","M"),
freq_new = c(49770, 47994),
means_new = c(.509, .491),
freq_old = c(1860,5091),
means_old = c(.268,.732),
index = c(53,149)
)
fulldata
percents<-fulldata[c(1,3,5)] %>% gather(variable, values, -gender )
percents
indexes<-fulldata[c(1,6)]
indexes
## try and get the numbers on there also.
ggplot(data=indexes, aes(x=gender, y=index)) +
geom_point(aes(fill='index'), show.legend=FALSE ) +
geom_text(aes(label = index), vjust = 1.6, color = "dark red", size = 2.5) +
scale_y_continuous(sec.axis = sec_axis(~.*1, name="Index")) +
geom_col(data=percents, aes(x=gender, y=values*100, group=variable, color = variable),
position=position_dodge2(reverse=TRUE)) +
scale_x_discrete(labels = c("Female",
"Male")) +
labs(title = "Gender",
y = "Percent")
### This doesn't do anything.
# scale_fill_manual(
# limits=c(1,0),
# values = c("#FF69B4","#4CBB17"),
# labels = c("New Customers","Old Customers"),
# name = element_blank())
### This outlines the legend key and labels the legends but that's it.
# scale_color_manual(
# limits=c(1,0),
# values = c("#FF69B4","#4CBB17"),
# labels = c("New Customers ","Old Customers"),
# name = element_blank())