0

I'm newbie in R and I'm trying to make a graphic with ggplot2 where you can 5 different stats. So, I would like to have on the right side a legend with the color and the name of the stadistic but I don't know how to do it.

My code to make the graphic are:

  ggplot(object@data, aes(x=data[,1])) +
    geom_point(aes(y=data[,2]), color="blue") + 
    geom_point(aes(y=data[,3]), color = "red") +
    geom_point(aes(y=data[,4]), color = "olivedrab4") +
    geom_point(aes(y=data[,5]), color = "hotpink4") +
    geom_point(aes(y=data[,6]), color = "limegreen") +
    labs(title = "Liga DIA: Comparativas (Liga Regular) - www.basketmetrics.com", 
         x = "Equipos", y = "Total") + 
    #Pone las etiquetas del eje de las X en vertical
    theme(axis.text.x = element_text(angle = 90, hjust = 1, color = "white"), 
          axis.text.y = element_text(color = "white"), 
          legend.position = "top", legend.title = element_blank()) + 
    #Modificamos el fondo del panel
    theme(panel.background = element_rect(fill = 'peachpuff', colour = 'white')) +
    theme(plot.background = element_rect(fill = 'navyblue', colour = 'white'),
          plot.title = element_text(size = 20, face = "bold", color = "white"), 
          axis.title.x = element_text(face = "bold", color = "white"),
          axis.title.y = element_text(face = "bold", color = "white")) +
    scale_color_manual(name="Estadísticas", labels = c("ORTG", "DRTG", "Pace", "eFG%", "3P%"),
                       values = c("blue", "red", "olivedrab4", "hotpink4", "limegreen"))

I supposed that with this code I could have the legend but it doesn't work to me :(

scale_color_manual(name="Estadísticas", labels = c("ORTG", "DRTG", "Pace", "eFG%", "3P%"),
                   values = c("blue", "red", "olivedrab4", "hotpink4", "limegreen"))

Right now, what I get is this graphic:

enter image description here

How can I set a legend on the right side with each color and the name of the statistic?

José Carlos
  • 2,850
  • 12
  • 59
  • 95
  • You get legends with mapped aesthetics. So map the aesthetics! – hrbrmstr Nov 14 '18 at 21:08
  • How can I do that? – José Carlos Nov 14 '18 at 21:10
  • Please start by reading any basic `ggplot` tutorial – Henrik Nov 14 '18 at 21:12
  • 1
    You'll want to first "gather" your plotted columns to move them from 5 separate columns to 2 columns, one saying which one it is (the "key") and one showing the value. Then you feed this to ggplot, where the key is mapped to an aesthetic, like color. Here's an example of those two steps: https://drsimonj.svbtle.com/plot-some-variables-against-many-others – Jon Spring Nov 14 '18 at 21:23

1 Answers1

1

You can start with these steps.
As stated by @Jon Spring, ggplot2 preferes the long format than the wide format of the data, so first you ought to transform your data, than apply the ggplot2 function.

# you can use this package, to melt the data. You have not given any data, so
# at the bottom of this question, you can see some fake data similar to your:
library(reshape2)
long <- melt(data, id.vars = c("name"))
long
   name variable value
1     a     col2     2
2     b     col2     2
3     c     col2     2
4     a     col3     3
5     b     col3     3
6     c     col3     3
7     a     col4     4
8     b     col4     4
9     c     col4     4
10    a     col5     5
11    b     col5     5
12    c     col5     5
13    a     col6     6
14    b     col6     6
15    c     col6     6

Now the plot:

ggplot(long, aes(x=long[,1],y =long[,3], color = long[,2])) +
# here only one geom_point, using the long format
geom_point() +      
labs(title = "Liga DIA: Comparativas (Liga Regular) - www.basketmetrics.com", x = "Equipos", y = "Total") + 
# you can manage the theme features in only one theme
theme(axis.text.x = element_text(angle = 90, hjust = 1, color = "white"), 
      axis.text.y = element_text(color = "white"), 
      legend.position = "top", legend.title = element_blank(),
      panel.background = element_rect(fill = 'peachpuff', colour = 'white'),
      plot.background = element_rect(fill = 'navyblue', colour = 'white'),
      plot.title = element_text(size = 20, face = "bold", color = "white"), 
      axis.title.x = element_text(face = "bold", color = "white"),
      axis.title.y = element_text(face = "bold", color = "white"),
      legend.key = element_rect(fill = "transparent", colour = "transparent")) +
      scale_color_manual(name="Estadísticas", labels = c("ORTG", "DRTG", "Pace", "eFG%", "3P%"),values = c("blue", "red", "olivedrab4", "hotpink4", "limegreen"))

enter image description here


With data:

data <- data.frame(name = c('a','b','c'),
                   col2 = c(2,2,2),
                   col3 = c(3,3,3),
                   col4 = c(4,4,4),
                   col5 = c(5,5,5),
                   col6 = c(6,6,6)
                   )
s__
  • 9,270
  • 3
  • 27
  • 45