Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add larger borders to shapes that have an already assigned size aesthetic in ggplot2?

Tags:

r

ggplot2

I am trying to plot several features of a group of samples to various aesthetics. Border color and size of a circle is one of them. However, if I try to set a larger baseline border for these shapes, by setting lwd=2, the shapes all revert to the same size and the legend is gone. I just want the circles to have a larger border, how can I do this?

An example:

library(ggplot2)
testFrame <- data.frame(
  sizeVar=factor(c('a', 'a', 'a', 'a', 'b', 'b', 'b', 'b')),
  samples=rep(c('Sample1', 'Sample2'), times=4),
  features=c(rep('Feature1', times=4), rep('Feature2', times=4))
)
testPlot <- ggplot(data=testFrame, aes(x=samples, y=features))
testPlot + 
    geom_point(aes(size=sizeVar), pch=21, color='black', fill='gray') +
    scale_size_manual(values=c(9,4)) + theme_bw()
testPlot + 
   geom_point(aes(size=sizeVar), pch=21, lwd=3, color='black', fill='gray') + 
   scale_size_manual(values=c(9,4)) + theme_bw()

Output without lwdenter image description here

like image 879
SeeLittle Avatar asked Jan 14 '13 22:01

SeeLittle


People also ask

How do you increase the size of points in Legend of ggplot2?

To change the Size of Legend, we have to add guides() and guide_legend() functions to the geom_point() function.

What does AES () do in ggplot?

aes() is a quoting function. This means that its inputs are quoted to be evaluated in the context of the data. This makes it easy to work with variables from the data frame because you can name those directly. The flip side is that you have to use quasiquotation to program with aes() .

What does size do in ggplot?

Colour and fillThe size of the filled part is controlled by size , the size of the stroke is controlled by stroke . Each is measured in mm, and the total size of the point is the sum of the two. Note that the size is constant along the diagonal in the following figure.


1 Answers

Start with your graph:

> testPlot + 
    geom_point(aes(size=sizeVar), pch=21, color='black', fill='gray') +
    scale_size_manual(values=c(9,4)) + theme_bw()

List the elements:

> grid.ls()

GRID.gTableParent.162
  background.1-6-6-1
  spacer.4-3-4-3
  panel.3-4-3-4
    grill.gTree.126
      panel.background.rect.121
      panel.grid.major.y.polyline.123
      panel.grid.major.x.polyline.125
    geom_point.points.116
    panel.border.rect.118
  axis-l.3-3-3-3
    axis.line.y.zeroGrob.136
    axis
  axis-b.4-4-4-4
    axis.line.x.zeroGrob.130
    axis
  xlab.5-4-5-4
  ylab.3-2-3-2
  guide-box.3-5-3-5
  title.2-4-2-4

Now to change the appropriate element:

> grid.edit("geom_point.points", grep=TRUE, gp=gpar(lwd=3))

enter image description here

like image 184
sebastian-c Avatar answered Sep 22 '22 17:09

sebastian-c