0

I want to plot two rates in the same plot using ggplot.

What I have done is the following

plot1 <- ggplot(table3, aes(x = issue_yr, y = share1, group = 1)) + 
           geom_point() + 
           geom_line(colour="red") +
           labs(title = "Charged Off rate for each year", y="%")

  plot1 + geom_point()+ geom_line(aes(x=issue_yr, y = share2), color = "blue")

But points for graph 2 does not show up. How can I add the points?

Any suggestion? enter image description here

Erick
  • 133
  • 2
  • 3
  • 16
  • 1
    I can only guess but try `plot1 + geom_point(aes(y = share2))+ geom_line(aes(x=issue_yr, y = share2), color = "blue")` – markus Oct 01 '18 at 19:58
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Oct 01 '18 at 20:00
  • @markus thanks, it worked – Erick Oct 01 '18 at 20:01
  • 2
    More broadly, you'll find ggplot easier to work with if you convert your data from "wide" to "long" so that instead of two columns `share1` and `share2` you have a column that denotes which share and then a single column of values. – joran Oct 01 '18 at 20:01
  • @joran thanks for your suggestion. I couldn't think of it before – Erick Oct 01 '18 at 20:03

1 Answers1

2

The last geom_line is the only geom here that is mapped to y = share2. You could either replace the last line with:

plot1 + 
  geom_point(aes(y = share2)) + 
  geom_line(aes(y = share2), color = "blue")

or, more idiomatically, you could convert your data to long (aka "tidy") format before you send it into ggplot. Something like the code below will combine your two columns that you want to plot (share1 and share2) into one column, distinguished by a new column called share_type.

table3 %>% 
  gather(share_type, value, share1:share2)

Then you'd map that field in your ggplot call, something like this, and it would plot both of your series, and add a legend.

plot1 <- ggplot(table3, aes(x = issue_yr, y = value, color = share_type)) + 
           geom_line() +  # Color of the line will be mapped based on share_type
           geom_point(color = "black")  # Assumes you want all points black

I think ggplot is quite elegant when you can use its built-in mapping to do the work for you. If you want to manually control the appearance of your various series, you can use scale_color_manual() to pick the colors you want to use.

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • I tried this but its not working. `table4 <- table3 %>% select(issue_yr, share1, share2) %>% gather(share_type, value, share1:share2) ggplot(table4, aes(x = issue_yr, y = value, color = share_type)) geom_line() + # Color of the line will be mapped based on share_type geom_point(color = "black")` – Erick Oct 01 '18 at 20:24
  • 1
    @Erick I might be wrong but it seems you are missing a `+` between `ggplot(...)` and `geom_line` in above comment. – markus Oct 01 '18 at 20:37