Two geom_points add a legend

前端 未结 4 1739
悲哀的现实
悲哀的现实 2020-11-29 06:45

I plot a 2 geom_point graph with the following code:

source(\"http://www.openintro.org/stat/data/arbuthnot.R\")
library(ggplot2)
ggplot() +
  geom_point(aes(         


        
4条回答
  •  悲哀的现实
    2020-11-29 07:33

    Here is a way of doing this without using reshape::melt. reshape::melt works, but you can get into a bind if you want to add other things to the graph, such as line segments. The code below uses the original organization of data. The key to modifying the legend is to make sure the arguments to scale_color_manual(...) and scale_shape_manual(...) are identical otherwise you will get two legends.

    source("http://www.openintro.org/stat/data/arbuthnot.R")
    library(ggplot2)
    library(reshape2)
    
    
    
    ptheme <- theme (
      axis.text            = element_text(size = 9),              # tick labels
      axis.title           = element_text(size = 9),              # axis labels
      axis.ticks           = element_line(colour = "grey70", size = 0.25),
      panel.background     = element_rect(fill = "white", colour = NA),
      panel.border         = element_rect(fill = NA, colour = "grey70", size = 0.25),
      panel.grid.major     = element_line(colour = "grey85", size = 0.25),
      panel.grid.minor     = element_line(colour = "grey93", size = 0.125),
      panel.margin         = unit(0 , "lines"),
      legend.justification = c(1, 0), 
      legend.position      = c(1, 0.1),
      legend.text          = element_text(size = 8),
      plot.margin          = unit(c(0.1, 0.1, 0.1, 0.01), "npc")   # c(bottom, left, top, right), values can be negative
    )
    
    cols    <- c( "c1" = "#ff00ff", "c2" = "#3399ff" )
    shapes  <- c("s1" = 16, "s2" = 17)
    
    p1 <- ggplot(data = arbuthnot, aes(x = year))
    p1 <- p1 + geom_point(aes( y = boys,  color = "c1", shape = "s1"))
    p1 <- p1 + geom_point(aes( y = girls, color = "c2", shape = "s2")) 
    p1 <- p1 + labs( x = "Year", y = "Rate" )
    p1 <- p1 + scale_color_manual(name = "Sex", 
                                    breaks = c("c1", "c2"), 
                                    values = cols,
                                    labels = c("boys", "girls"))
    p1 <- p1 + scale_shape_manual(name = "Sex", 
                                  breaks = c("s1", "s2"),
                                  values = shapes,
                                  labels = c("boys", "girls"))
    p1 <- p1 +  ptheme
    
    print(p1)
    

    output results

提交回复
热议问题