Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

point size in ggplot 2.0.0

Tags:

r

ggplot2

I'm having trouble recreating a plot since I updated to ggplot version 2.0.0: It seems I can't reduce the point size as much as before, which is a problem in a plot with very many points. In the below examples, there is a reasonable difference in point size between plot1 and plot2, the point size in plot3 is at least a little bit smaller, but between plot3 and plot4 there's no difference in point size:

df <- data.frame(x=1:10, y=runif(10))
pl <- ggplot(df) +
    geom_point(aes(x,y), size=1)
ggsave("plot1.png", plot=pl, width=14, height=7, units="cm", dpi=1200 )

pl <- ggplot(df) +
    geom_point(aes(x,y), size=0.1)
ggsave("plot2.png", plot=pl, width=14, height=7, units="cm", dpi=1200 )

pl <- ggplot(df) +
geom_point(aes(x,y), size=0.01)
ggsave("plot3.png", plot=pl, width=14, height=7, units="cm", dpi=1200 )

pl <- ggplot(df) +
geom_point(aes(x,y), size=0.001)
ggsave("plot4.png", plot=pl, width=14, height=7, units="cm", dpi=1200 )

In the previous version of ggplot2 I had used a point size of 0.25 and it looked way smaller than it does now, which is why I tried to further reduce it using the new ggplot2 version. Do I miss a change in the code of the new version? Couldn't find anything in the documentation...

like image 401
silkita Avatar asked Jan 06 '16 17:01

silkita


People also ask

How do I change the point type in ggplot2?

You can change the number to plot different shapes, i.e. geom_point(shape = x) . If you want to change point shapes based on a grouping variable, then first set the shape with the grouping variable in geom_point and then use scale_shape_manual to choose the desired shapes (optional).

What is the default size of geom_point?

geom_path has a default size of 0.5 #4094.

What is %>% in Ggplot?

the %>% is a pipe operator that is actually part of the dplyr library (along with the filter function) not from the ggplot2 library. To sample 1%, there is a sample_frac function in the dplyr library. It would be something like (df %>% sample_frac(0.01))

What does geom_point mean in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot. You'll learn a whole bunch of them throughout this chapter. Each geom function in ggplot2 takes a mapping argument.


2 Answers

Ok, I've found the solution. As pointed out by @henrik and @silkita now the default shape has changed from 16 to 19 in the latest ggplot2 release. And as you can see in the documentation (for example here) the shape '19' is slightly larger than '16'. But this is not the reason why "points" are larger in version 2.0.0. Looking at the ggplot2 source of geom-point.R for the latest release we can see that:

default_aes = aes(
    shape = 19, colour = "black", size = 1.5, fill = NA,
    alpha = NA, stroke = 0.5
  )

While in the previous releases it was:

default_aes <- function(.) aes(shape=16, colour="black", size=2, fill = NA, alpha = NA)

Then, to have the small point as before we should put stroke to zero. To summarise, to obtain the smallest point you should write:

geom_point(size = 0.1) # ggplot2 before 2.0.0
geom_point(size = 0.1, stroke = 0, shape = 16) # ggplot2 2.0.0

By the way, when working with smallest points there is no difference between using different shapes (a pixel remains a pixel).

UPDATE: As pointed out on Twitter by Hadley Wickham this change was explained in the release notes

like image 148
Matteo De Felice Avatar answered Oct 07 '22 11:10

Matteo De Felice


Try using the shape parameter:

n <- 10000
df <- data.frame(x=1:n, y=runif(n))
pl <- ggplot(df) +
  geom_point(aes(x,y), size=1,shape=".") + labs(title="shape='.',size=1")
pl

yields:

enter image description here

while:

pl <- ggplot(df) +
  geom_point(aes(x,y), size=1) + labs(title="size=1")
pl

yields: - (and it is the same for all smaller sizes)

enter image description here

like image 24
Mike Wise Avatar answered Oct 07 '22 12:10

Mike Wise