0

I am trying to edit legend using gplot2 in R. Here is my dataframe (data):

Gene    Age Value   Values
GeneX   42.5    2.5 0.011
GeneX   47.5    2.5 0.017
GeneX   52.5    2.5 0.012
GeneX   57.5    2.5 0.004
GeneX   62.5    2.5 0.010
GeneX   67.5    2.5 0.017
GeneX   72.5    2.5 0.014
GeneX   42.5    5   0.016
GeneX   47.5    5   0.019
GeneX   52.5    5   0.017
GeneX   57.5    5   0.006
GeneX   62.5    5   0.012
GeneX   67.5    5   0.020
GeneX   72.5    5   0.017
GeneX   42.5    10  0.019
GeneX   47.5    10  0.020
GeneX   52.5    10  0.020
GeneX   57.5    10  0.017
GeneX   62.5    10  0.019
GeneX   67.5    10  0.019
GeneX   72.5    10  0.018
GeneX   42.5    25  0.022
GeneX   47.5    25  0.023
GeneX   52.5    25  0.024
GeneX   57.5    25  0.023
GeneX   62.5    25  0.024
GeneX   67.5    25  0.026
GeneX   72.5    25  0.022
GeneX   42.5    50  0.028
GeneX   47.5    50  0.029
GeneX   52.5    50  0.029
GeneX   57.5    50  0.031
GeneX   62.5    50  0.030
GeneX   67.5    50  0.033
GeneX   72.5    50  0.032
GeneX   42.5    75  0.036
GeneX   47.5    75  0.036
GeneX   52.5    75  0.038
GeneX   57.5    75  0.042
GeneX   62.5    75  0.040
GeneX   67.5    75  0.043
GeneX   72.5    75  0.040
GeneX   42.5    90  0.045
GeneX   47.5    90  0.043
GeneX   52.5    90  0.046
GeneX   57.5    90  0.048
GeneX   62.5    90  0.052
GeneX   67.5    90  0.055
GeneX   72.5    90  0.057
GeneX   42.5    95  0.062
GeneX   47.5    95  0.048
GeneX   52.5    95  0.050
GeneX   57.5    95  0.059
GeneX   62.5    95  0.066
GeneX   67.5    95  0.057
GeneX   72.5    95  0.058
GeneX   42.5    97.5    0.090
GeneX   47.5    97.5    0.067
GeneX   52.5    97.5    0.064
GeneX   57.5    97.5    0.063
GeneX   62.5    97.5    0.079
GeneX   67.5    97.5    0.061
GeneX   72.5    97.5    0.059

My code for making the scatter plot is this:

library(ggplot2)
library(dplyr)

graph <- data %>%
    ggplot(aes(x = data$Age,
        y = data$Values,
        group = data$Value))
graph <- graph +
    geom_line(aes(color = data$Value), na.rm = TRUE) +
    geom_point(aes(color = data$Value),
                size = 1,
                na.rm = TRUE) +
    labs(title="GeneX", x="Groups", y="Values") + 

    scale_x_continuous(breaks = c(42.5, 47.5, 52.5, 57.5, 62.5, 67.5, 72.5),
        labels = c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7")) +
    scale_y_continuous(breaks = c(0, 0.025, 0.050, 0.075, 0.100),
        labels = c("0", "25", "50", "75", "100"),
        limits = c(0,0.105)) +
    theme(legend.title=element_blank()) +
     guides(fill = guide_legend(title = "On Treatment"))

My graph currently looks like this:

enter image description here

Is it possible to also add the Value 2.5, 5, 10, 90, 95 and 97.5 in the legend along with the ones that are already there (25, 50, 75). I also need to add a title to my legend which is "Value nth". I have already done some googling but I am not able to work it out.

Any help appreciated. Thank you.

Letin
  • 1,255
  • 5
  • 20
  • 36
  • 1
    Have you tried setting the breaks for your color scale, much the same as you've set breaks for your x and y scales? – camille Jul 16 '19 at 15:25
  • @Camille, would you be able to indicate where to use these lines of code. I did try things like scale_fill_discrete to add a name or break. It produces graph, but does nothing. Would you be able to indicate that in my code? – Letin Jul 16 '19 at 15:29
  • 1
    You assigned to color, not fill, so adjusting a fill scale won't change anything. The same as you have a `scale_x_continuous`, you can set a `scale_color_continuous`. Also [don't use `$` inside `aes`](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio) – camille Jul 16 '19 at 15:38
  • 1
    Also, not totally related, but scatterplot generally means points representing 2 continuous variables, often a dependent & independent variable – camille Jul 16 '19 at 15:40
  • @Camille, thanks a ton. This worked. I am just trying to adjust the area as the legend seems too cluttered now. – Letin Jul 16 '19 at 15:57
  • @Camille, I also agree with your comment that scatter plots should represent continuous variables. This was just a demo to get an answer for what I needed. Thank you. – Letin Jul 16 '19 at 16:07

1 Answers1

1

One way to deal with the now cluttered legend is to increase its height using legend.key.height:

library(dplyr)
library(readr)
library(ggplot2)

"Gene   Age   Value   Values
GeneX   42.5    2.5  0.011
GeneX   47.5    2.5  0.017
GeneX   52.5    2.5  0.012
GeneX   57.5    2.5  0.004
GeneX   62.5    2.5  0.010
GeneX   67.5    2.5  0.017
GeneX   72.5    2.5  0.014
GeneX   42.5    5    0.016
GeneX   47.5    5    0.019
GeneX   52.5    5    0.017
GeneX   57.5    5    0.006
GeneX   62.5    5    0.012
GeneX   67.5    5    0.020
GeneX   72.5    5    0.017
GeneX   42.5    10   0.019
GeneX   47.5    10   0.020
GeneX   52.5    10   0.020
GeneX   57.5    10   0.017
GeneX   62.5    10   0.019
GeneX   67.5    10   0.019
GeneX   72.5    10   0.018
GeneX   42.5    25   0.022
GeneX   47.5    25   0.023
GeneX   52.5    25   0.024
GeneX   57.5    25   0.023
GeneX   62.5    25   0.024
GeneX   67.5    25   0.026
GeneX   72.5    25   0.022
GeneX   42.5    50   0.028
GeneX   47.5    50   0.029
GeneX   52.5    50   0.029
GeneX   57.5    50   0.031
GeneX   62.5    50   0.030
GeneX   67.5    50   0.033
GeneX   72.5    50   0.032
GeneX   42.5    75   0.036
GeneX   47.5    75   0.036
GeneX   52.5    75   0.038
GeneX   57.5    75   0.042
GeneX   62.5    75   0.040
GeneX   67.5    75   0.043
GeneX   72.5    75   0.040
GeneX   42.5    90   0.045
GeneX   47.5    90   0.043
GeneX   52.5    90   0.046
GeneX   57.5    90   0.048
GeneX   62.5    90   0.052
GeneX   67.5    90   0.055
GeneX   72.5    90   0.057
GeneX   42.5    95   0.062
GeneX   47.5    95   0.048
GeneX   52.5    95   0.050
GeneX   57.5    95   0.059
GeneX   62.5    95   0.066
GeneX   67.5    95   0.057
GeneX   72.5    95   0.058
GeneX   42.5    97.5 0.090
GeneX   47.5    97.5 0.067
GeneX   52.5    97.5 0.064
GeneX   57.5    97.5 0.063
GeneX   62.5    97.5 0.079
GeneX   67.5    97.5 0.061
GeneX   72.5    97.5 0.059" %>% 
  read_table() %>% 
  ggplot(aes(x = Age,
             y = Values,
             group = Value)) +
  geom_line(aes(color = Value), na.rm = TRUE) +
  geom_point(aes(color = Value), size = 1, na.rm = TRUE) +
  labs(title="GeneX", x="Groups", y="Values") + 
  scale_x_continuous(breaks = c(42.5, 47.5, 52.5, 57.5, 62.5, 67.5, 72.5),
                     labels = c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7")) +
  scale_y_continuous(breaks = c(0, 0.025, 0.050, 0.075, 0.100),
                     labels = c("0", "25", "50", "75", "100"),
                     limits = c(0,0.105)) +
  scale_color_continuous(name = "Value nth", 
                         breaks = c(2.5, 5, 10, 25, 50, 75, 90, 95, 97.5)) + 
  theme(legend.key.height = unit(3, "cm"))

enter image description here

the-mad-statter
  • 5,650
  • 1
  • 10
  • 20