-1

Image of legend i would like to add to

I would like to know how can i add a simple observation number (n) in legend of this scatter plot in ggplot2

library(readr)
library(ggplot2)
library(dplyr)
All.mutations.no.inserts <- read_csv("All mutations no inserts.csv")
All.mutations.no.inserts$Fungicide <- factor(All.mutations.no.inserts$Fungicide, levels = c("SDHI 1",
                                                                   "SDHI 2",
                                                                   "SDHI 3",
                                                                   "SDHI 4",
                                                                   "SDHI 5",
                                                                   "SDHI 6",
                                                                   "SDHI 7",
                                                                   "SDHI 8",
                                                                   "SDHI 9",
                                                                   "SDHI 10",
                                                                   "SDHI 11",
                                                                   "SDHI 12"))
All.mutations.no.inserts$SDH.mutation <- factor(All.mutations.no.inserts$`SDH.mutation`)
ggplot(All.mutations.no.inserts, aes(x = Fungicide, y = EC50, color = SDH.mutation)) + 
  geom_point(size = 4) +
  scale_y_log10() +
  theme_minimal() +
  theme(axis.text.x=element_text(angle = -90, hjust = 0),
        axis.title.x=element_blank()) 

How should i modify my code?

  • 2
    Welcome to SO! Please consider to create a minimal [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – markus Feb 05 '19 at 23:46
  • THanks, i am a compleate noob, i have edited post and inserted my code in it – Artūrs Katamadze Feb 06 '19 at 09:55

1 Answers1

1

here's an example using dplyr. See the comments in the code.

library(dplyr)
library(ggplot2)

# sample data set
expand.grid(y = rnorm(20),
            x = letters[1:5],
            z = letters[6:10]) %>% 
  sample_frac(0.75) %>% 
  # add column n with counts for each group
  add_count(z) %>% 
  # combine the group z and count n into one column
  mutate(zn = paste0(z, ' (', n, ')')) %>% 
  # plot as you had
  ggplot(aes(x, y, colour = zn)) +
  geom_point() +
  # rename the legend title
  labs(colour = 'z (# obs)')

Created on 2019-02-06 by the reprex package (v0.2.1)

rdh
  • 1,035
  • 7
  • 11