0

I have successfully made a dotplot in R using ggplot2 using this code.

data <- data.frame("State" = c("NJ", "NY", "PA", "ND","NJ", "NY", "PA", "ND"), "Year" = c("1985", 
"1985", "1985", "1985", "1990", "1990", "1990", "1990"), "DataValue" = c(33, 20, 44, 21, 55, 11, 17, 
46))


p <- ggplot(data,aes(x = State, y = DataValue, color = Year)) + 
  geom_point(data = subset(data, !is.na(DataValue))) + 
  geom_point(data = subset(data, is.na(DataValue)), color = "pink") +
  scale_shape_manual(values=c(4, 17))+
  scale_color_manual(values=c("cyan", "blueviolet", "purple", "turquiose4"))  + 
  theme(axis.text.x = element_text(colour = "black"), plot.title = element_text(hjust = 0.5)) +
  geom_vline(xintercept = c(3.5)) + labs(title = "DataValues", x = "State/Territory", y = "Value") +
  geom_hline(yintercept = 0, color = "grey24")
p<- p + theme(legend.position="bottom")
p

I added the shape_scale_manual to change the shape depending on the year and it is not working. Does anyone have any suggestions on how I can fix this?

Thank you!

1
  • You may use one of either shape, size or color to identify a group but not multiple. The ggplot2 logic is that if you ex. use color for Variable 1 then you have a legend that explains what each color corresponds to while keeping the shape constant and then if you ex. use shape for Variable 2 then you may add another legend that explains what each shape corresponds to while keeping the color constant. Commented Sep 23, 2020 at 18:07

1 Answer 1

1

Maybe this what you are looking for:

  1. To make scale_shape_manual work you have to map a variable on shape, i.e. map Year on shape.

  2. If you are using different datasets and or aesthetics for the geoms in my opinion it's best to make them local instead of setting them globally in ggplot(), i.e. move the mapping on color and shape into the first geom_point. This should give you the right colors and shapes.

data <- data.frame("State" = c("NJ", "NY", "PA", "ND","NJ", "NY", "PA", "ND"), "Year" = c("1985", 
                                                                                          "1985", "1985", "1985", "1990", "1990", "1990", "1990"), "DataValue" = c(33, 20, 44, 21, 55, 11, 17, 
                                                                                                                                                                   46))


library(ggplot2)

p <- ggplot(mapping = aes(x = State, y = DataValue)) + 
  geom_point(data = subset(data, !is.na(DataValue)), aes(color = Year, shape = Year)) + 
  geom_point(data = subset(data, is.na(DataValue)), color = "pink") +
  scale_shape_manual(values=c(4, 17))+
  scale_color_manual(values=c("cyan", "blueviolet", "purple", "turquiose4"))  + 
  theme(axis.text.x = element_text(colour = "black"), plot.title = element_text(hjust = 0.5)) +
  geom_vline(xintercept = c(3.5)) + labs(title = "DataValues", x = "State/Territory", y = "Value") +
  geom_hline(yintercept = 0, color = "grey24")
p<- p + theme(legend.position="bottom")
p

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.