开发者

How to remove the background and labels when we have two geoms in ggplot2

开发者 https://www.devze.com 2023-03-15 05:09 出处:网络
I am developing a graph in R with ggplot2 that has two geoms (one geom_line and one geom_text). It draws a line graph and then places text labels on start and end points of ea开发者_JAVA技巧ch line se

I am developing a graph in R with ggplot2 that has two geoms (one geom_line and one geom_text). It draws a line graph and then places text labels on start and end points of ea开发者_JAVA技巧ch line segment.

  (myplot <- ggplot(data=datatable, aes(x, y, group = group,colour = group, label=mylabels)) + geom_line(size = 1.5))  
  myplot + geom_text(color = "black") 

Now my question is how can I do the following tasks in ggplot2, they all work when I only have one geom but not with both (seems that they overide each other)

1 - making the background white. The following code works with geom_line but as soon as I add geom_text it becomes gray again. Even if I add this line after geom_text it gets rid of the point labels that are on the chart.

myplot + opts(panel.background = theme_rect(fill = "white", colour = NA)) 

2- x labels and x label format. Again the following code works with only one geom but breaks when I have the second geom

myplot + scale_x_date(format="%m", 'my x label')

3- While we are on it how can I put the legend at the bottom and spread it horizontally (p + opts(legend.position="bottom")) spreads that vertically that looks very stupid.


For 1), you haven't saved the object myplot after the second and third calls involving it. This works for me:

set.seed(3)
dat <- data.frame(x = runif(10), y = rnorm(10), group = gl(2,5),
                  mylabel = paste(1:10, "foo"))

require(ggplot2)
myplot <- ggplot(data=dat, aes(x, y, group = group, colour = group, 
                 label = mylabel)) + geom_line(size = 1.5)

myplot + geom_text(color = "black") + 
    opts(panel.background = theme_rect(fill = "white", colour = NA))

Note that I only ever save myplot once. The second call involving myplot modifies it on the fly but doesn't save it.

For the rest, you'll need to provide a reproducible example.

0

精彩评论

暂无评论...
验证码 换一张
取 消