数据框标题中的百分比符号

时间:2018-07-13 03:52:04

标签: r dataframe

我的标头中包含%符号。在第二列中为“ 95%percentile”,我尝试通过将其设置为“ 95%_percentile”来克服空间。问题是ggplot无法读取它,我该如何克服呢?谢谢你。

data2 <- read.csv("percentile.csv", head = TRUE, stringsAsFactors = TRUE)
head(data2)

   Stations X95._percentile
 1       GM          48.700
 2       GG          53.445
 3      Ari          49.500
 4      Lal          51.500
 5      Dab          49.000
 6      K.K.         57.000

str(data2)
# 'data.frame': 15 obs. of  2 variables:
#  $ Stations       : Factor w/ 15 levels "Ari","Bach",..: 6 5 1 10 4 8 11 12 13 14 ...
#  $ X95._percentile: num  48.7 53.4 49.5 51.5 49 ...

# Horizontal lollipop plot
 ggplot(data2, aes(x=Stations, y=95._percentile)) +
 geom_segment( aes(x=Stations, xend=Stations, y=0, yend=95._percentile), 
 color="skyblue") +
 geom_point( color="blue", size=3, alpha=0.6) +
 theme_light() +
 coord_flip() +
 theme(
   panel.grid.major.y = element_blank(),
   panel.border = element_blank(),
   axis.ticks.y = element_blank()
 ) 

# Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?

1 个答案:

答案 0 :(得分:1)

这是由于此处的错字:     ggplot(data2, aes(x=Stations, y=95._percentile)) + geom_segment( aes(x=Stations, xend=Stations, y=0, yend=95._percentile),

您打算     ggplot(data2, aes(x=Stations, y=X95._percentile)) + geom_segment( aes(x=Stations, xend=Stations, y=0, yend=X95._percentile),

更改之后,您会得到一个不错的图: enter image description here