简体   繁体   中英

Vertical Lines for Data Points in geom_point in ggplot2 for R

Is there any way to get vertical lines as the data point representations in ggplot2? None of the shape options 0-25 are what I'm looking for, so I'm guessing I need another add-in or a way to change the dimensions of shape 15. The left chart here is an example of what I want it to look like.

Here's some simple code in case it's helpful in writing the response:

a <- c(1, 3, 5)
b <- c(2, 4, 6)
df <- data.frame(a, b)

ggplot(data = df, aes(x = a, y = b)) +
geom_point(shape = 15)

I see two possible methods to help produce a vertical glyph similar to what you see in the referenced plot.

The first is a workaround and makes use of the geom_errorbar() function. The second involves passing specific numeric values to the shape parameter, but with the help of the scale_shape_identity() function.

# Reproducing your data

a <- c(1, 3, 5)
b <- c(2, 4, 6)
df <- data.frame(a, b)

# Let's specify the upper and lower limits manually

df <- df %>%
  mutate(
    upper = +0.5 + b,
    lower = -0.5 + b
  )

The first approach exploits the geom_errorbar() function, except we set width = 0 to suppress the horizontal lines. Note, I set the upper and lower limits manually and appended them to your data frame. Adjust the vertical distances to suit your needs.

# 1st Method

ggplot(data = df, aes(x = a, y = b)) +
  geom_errorbar(aes(ymin = lower, ymax = upper), color = "red", width = 0) +
  theme_classic()

错误栏解决方法

The second approach exploits the shape parameter directly. Note, the geom_point() function's shape parameter accommodates a variety of different values. Shape values from 0 to 25 are commonly used, but others exist. In particular, shapes 32 to 127 correspond to various ASCII characters. Layer on scale_shape_identity() and you may pass through any legal shape value. The value 73 or 108 should work well.

To offer further insight, the numeric values we pass to the shape parameter are also referred to as ASCII codes. In particular, code 73 corresponds to the uppercase letter "I" (ie, "I" ) and code 108 corresponds to the lowercase letter "l" (ie, "l" ). In your plot the letters will appear without the serifs (ie, crossbars), transforming all your points into vertical bars. See below for a demonstration:

# 2nd Method

ggplot(data = df, aes(x = a, y = b)) + 
  scale_shape_identity() +
  geom_point(shape = 108, size = 10, color = "red") +
  theme_classic()

形状值 108

In fact, passing in any of the following characters will produce a vertical line: "|" , "l" , or "I" .

# Other possible solutions

geom_point(shape = "I")  # ASCII code 73
geom_point(shape = "l")  # ASCII code 108
geom_point(shape = "|")  # ASCII code 124

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