Mongzyy Mongzyy - 2 months ago 16
R Question

Modifying Aesthetics - ggplot2

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

enter image description here

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.


Answer

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)

enter image description here

Comments