1

1]

We are given data on the annual income along with ages. A part of the problem is to "Draw a green dashed line separating people whose annual income is in the upper half from the rest." I know how to format the line but not sure how to code it that it is to separate the data (Which I believe should be placed at the astrik).

In addition, the graph that does show, does not seem to have a smoothing line. Is it because it is hidden behind the points of is it not graphed?

ggplot() +
  geom_point(data = ss16wa, aes(AGEP, WAGP, color = SEX, shape = CIT,  alpha = 0.5)) +
  geom_smooth() +
  geom_line(color = "green", linetype = "dashed" * ) + 
  xlab("Age") + ylab("Annual income, $")
Richard Telford
  • 9,558
  • 6
  • 38
  • 51
Dianna Li
  • 121
  • 6
  • can you provide a reproducible example of your dataset ? (see here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Mar 05 '20 at 05:44
  • You're looking for a reference line so you see `geom_abline`. – Rohit Mar 05 '20 at 05:45
  • You passed data only to geom_point. If you want the data to be used by all the from function u should specify it with ggplot2() – StupidWolf Mar 05 '20 at 06:52

1 Answers1

1

You can use geom_hline to draw a horizontal line that will match the median of the income:

library(ggplot2)
ggplot(df, aes(x = age, y = value, color= sex))+
  geom_point()+
  geom_smooth()+
  geom_hline(yintercept = median(df$value), linetype = "dashed", color = "green", size = 2) + 
  xlab("Age") + ylab("Annual income, $")

enter image description here

Is it what you are looking for ?


NB: Regarding your question about geom_smooth not display, it is difficult to answer it without a reproducible example. Please provide a reproducible example in order people can try to solve our issue with geom_smooth

NB: You don't need to add alpha = 0.5 into aes as you are using a fixed value


Reproducible example

df <- data.frame(value = sample(1:10000,500,replace = TRUE),
                 age = sample(20:65,500, replace = TRUE),
                 sex = sample(c("M","F"),500, replace = TRUE))
dc37
  • 15,840
  • 4
  • 15
  • 32