Home > Enterprise >  How to directly label regression lines within plot frame (without a legend)?
How to directly label regression lines within plot frame (without a legend)?

Time:07-15

Data and Previous Content

This question is a continuation of a enter image description here

Instead of coloring a specific regression line, now I want to add a direct label to the plot window like above. I know that by faceting the data, we can achieve this with a legend, coloring the lines, etc. We can even manually add an annotation by selecting the x and y coordinates with annotate or geom_text.

But I want something that doesn't require a legend or manually figuring out where the exact geom coordinates are. Is there a way to simply add the label to a regression line within the plot window similar to other aes functions? This is the base plot I have so far, with the label now removed and regression lines colored:

ggplot(slack.work,
       aes(x=Coffee_Cups,
           y=Mins_Work,
           color=Month_Name)) 
  geom_point(alpha = .4) 
  geom_smooth(method = "lm",
              se = F) 
  scale_colour_viridis_d() 
  theme_bw() 
  labs(title = "Coffee Cups x Minutes of Productivity",
       subtitle = "Pearson r = .30, p < .001",
       x="Cups of Coffee",
       y="Minutes of Work",
       color="Month") 
  theme(plot.title = element_text(face = "bold",
                                  size = 15,
                                  family = "mono"),
        plot.subtitle = element_text(face = "italic"),
        legend.position = "none")

Currently, it looks like this:

enter image description here

But I would like for it to look something like this:

enter image description here

CodePudding user response:

Adapting enter image description here

  • Related