简体   繁体   中英

Visualizing two or more data points where they overlap (ggplot R)

I have a scatterplot that has colour-coded data points. When two or more of the data points overlap only one of the colours is shown (whichever is first in the legend). Each of these data points represents an item and I need to show which items fall at each point on the scale. I'm using R (v.3.3.1). Would anyone have any suggestions as per how I could show that there are multiple items at each point on the scatterplot? Thanks in advance.

pdf('pedplot.pdf', height = 6, width = 10)
p3 <- ggplot(data=e4, aes(x=e4$domain, y=e4$ped)) + geom_point(aes(color = 
    e4$Database_acronym), size = 3, shape = 17) + 
    labs(x = "Domains", y = "Proportion of Elements per Domain", color = "Data 
    Sources") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) 
p3 dev.off();

Separately from or in addition to jittering as mentioned here, you could also consider making the points partially transparent:

linecolors <- c("#714C02", "#01587A", "#024E37")
fillcolors <- c("#9D6C06", "#077DAA", "#026D4E")

# partially transparent points by setting `alpha = 0.5`
ggplot(mpg, aes(displ, cty, colour = drv, fill = drv)) +
  geom_point(position=position_jitter(h=0.1, w=0.1),
             shape = 21, alpha = 0.5, size = 3) +
  scale_color_manual(values=linecolors) +
  scale_fill_manual(values=fillcolors) +
  theme_bw()

在此处输入图片说明

You could jitter the points, meaning add a bit of noise to remove the overlap (probably the most commonly used option). Another option, would be to use different marker shapes (plus a small size adjustment) chosen so that the markers will be visible when plotted on top of each other. This will work if you have only two or three different marker types. A third option is to vary the size for each color, once again only for cases with maybe two or three colors/sizes, though the size difference might be confusing. If you can have multiple points of the same color with the same coordinates, then only jitter (among the three options above) will make that apparent. In any case, here are examples of each approach:

dat = data.frame(x=1:5, y=rep(1:5,3), group=rep(LETTERS[1:3],each=5))
theme_set(theme_bw())

# Jitter
set.seed(3)
ggplot(dat, aes(x,y, colour=group)) +
  geom_point(size=3, position=position_jitter(h=0.15,w=0.15))

# Vary the marker size
ggplot(dat, aes(x,y, colour=group,size=group)) +
  geom_point() +
  scale_color_manual(values=c("red","blue","orange")) +
  scale_size_manual(values=c(5,3,1))

# Vary the marker shape (plus a small size adjustment)
ggplot(dat, aes(x,y, colour=group, size=group, shape=group)) +
  geom_point(stroke=1.5) +
  scale_colour_manual(values=(c("black", "green", "orange"))) +
  scale_shape_manual(values=c(19,17,4)) +
  scale_size_manual(values=c(4,3,3))

在此处输入图片说明

What about using different shapes and fills?

ggplot(mpg, aes(displ, cty,  fill = drv, shape = drv)) +
     geom_point(position=position_jitter(h=0.1, w=0.1), alpha = 0.5, size = 3) +
     scale_fill_manual(values=c("red","blue","orange")) +
     scale_shape_manual(values= c(23, 24, 25)) +
     theme_bw()

在此处输入图片说明

Try geom_point(aes(color = e4$Database_acronym), position = "jitter", size = 3, shape = 17) .

This adds a little bit of random variation to your scatter plot and thereby prevents overplotting.

Another option could be by counting the overlapping points using geom_count with scale_size_area to scale the sizes of the points. Here is some reproducible code:

library(ggplot2)
ggplot(mpg, aes(x = displ, y = cty)) +
  geom_count() +
  scale_size_area()

Also, an example when using a color aesthetic to see the difference of counts of groups:

ggplot(mpg, aes(x = displ, y = cty, colour = drv)) +
  geom_count() +
  scale_size_area()

Created on 2023-01-31 with reprex v2.0.2

You could change the number of breaks in scale_size_area to show different sizes. Please check the link above for more examples.

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