简体   繁体   中英

Modifying Aesthetics - ggplot2

I am trying to learn ggplot2 and have made below plots:

在此处输入图片说明

Using this code:

library(ggplot2); library(gridExtra)
gg <- ggplot(mydata,aes(x=Level))
plot1 <- gg + geom_line(aes(y=Experience,colour="xp"),size=1) +
    labs(title="xp") 
g <- ggplot(mydata,aes(x=Level))
plot2 <- g + geom_line(aes(y=Experience,colour="xp"),size=1) + geom_line(aes(y=Accu,colour="accu"),size=1) +
    labs(title="xp vs Accumulated") 
grid.arrange(plot1,plot2,ncol=2)

Where mydata is a data frame containing 3 columns ( Level , xp and accu ) and 30 rows.

What I am wondering is:

  1. How to get the y-axis on the left-hand plot to have the same form as the right-hand plot.
  2. How to make the color of "xp" the same in both plots without removing the descriptions of what the lines represent.

How about this (with some random data)?

library(ggplot2) 
library(gridExtra)
library(scales)

gg <- ggplot(mydata,aes(x=Level))
plot1 <- gg + geom_line(aes(y=Experience,colour="xp"),size=1) +
  labs(title="xp") + scale_y_continuous(labels = comma) +   
  scale_colour_manual(values = c("red"))

g <- ggplot(mydata,aes(x=Level))
plot2 <- g + geom_line(aes(y=Experience,colour="xp"),size=1) + 
   geom_line(aes(y=Accu,colour="accu"),size=1) +
   labs(title="xp vs Accumulated")  + scale_y_continuous(labels = comma) + 
   scale_colour_manual(values = c("blue", "red"))

grid.arrange(plot1,plot2,ncol=2)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM