2

I am trying to figure out a way to color my point on a geom_point plot based upon the type of transmission, but in the mpg dataset, the trans column has different names for auto and manual trans. How can I rename the values in the trans column to be either Auto for automatic and Manual for manual transmissions? I also attached a picture of the desired graph for reference.

Here is my main plot code:

data <- mpg
n <- nrow(mpg)
mpg_auto <- subset(mpg, substring(trans[1:n],1,1)=="a")
mpg_manual <- subset(mpg, substring(trans[1:n],1,1)=="m")

mpg$trans <- factor(mpg$trans, levels=c(mpg_auto,mpg_manual),
                labels = c("0","1"))

mpg_select <- subset(mpg, mpg$hwy > 30 & mpg$displ < 3)
mpg_select <- as.data.frame(mpg_select)

gg<-  ggplot(mpg) + aes(x = displ, y = hwy) + 
  geom_point(aes(col = trans))+
  geom_encircle(aes(x = displ, y = hwy), 
            data = mpg_select, 
            color= "darkgreen",
            expand = .05,
            size = 2) + 
  annotate_textp(x = .2, y = .9, size = 15,
             label = "Efficient Vehicle", color = "darkgreen")+
  labs(y = "Hwy MPG",
       x = "Displacement")

  ggMarginal(gg, type= "density", alpha = 0.5,
       groupColour = TRUE, groupFill = TRUE)

Picture of the plot with the above code: https://ibb.co/fGMSXdn
  • 1
    Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Mar 16 '19 at 00:22
  • 2
    @Tung they're using the mpg dataset which is in `ggplot2`. OP, it's hard to help without any code. What have you tried so far, and how hasn't it worked? – camille Mar 16 '19 at 00:51
  • Here is a link for the full code and a picture of my graph so far. https://ibb.co/fGMSXdn – Gavyn Henderson Mar 16 '19 at 03:07
  • @Z.Lin Got it post has been updated – Gavyn Henderson Mar 16 '19 at 03:26
  • Since Gregor's answer works for you, you should mark it as correct. Since you're the question's author, only you can mark the question as answered. Click on the check mark you see by his answer. – shea Mar 18 '19 at 05:54

1 Answers1

3

Here's a good way to relabel the transmission (I create a new column named transmission, but you could just as easily overwrite the existing column).

mpg$transmission = ifelse(substring(mpg$trans, 1, 1) == "a", "automatic", "manual")

Now that's done, coloring is easy:

gg <- ggplot(mpg, aes(x = displ, y = hwy) + 
  geom_point(aes(color = transmission))+
  labs(y = "Hwy MPG",
       x = "Displacement")

enter image description here

I've left out all your non-standard ggplot stuff because I'm not sure what package(s) it's from. It doesn't seem related to your issue anyway, so you should be able to just add it back in.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294