Siti Sal
Siti Sal

Reputation: 119

Percentage symbol in data frame header

I'm having % symbol in my header. which is '95% percentile' on second column, I try to overcome the space by make it '95%_percentile' instead. The problem is ggplot cant read it , how can I overcome this? Thank you..

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?

Upvotes: 0

Views: 544

Answers (1)

markhogue
markhogue

Reputation: 1179

This was due to a typo here: ggplot(data2, aes(x=Stations, y=95._percentile)) + geom_segment( aes(x=Stations, xend=Stations, y=0, yend=95._percentile),

You intended ggplot(data2, aes(x=Stations, y=X95._percentile)) + geom_segment( aes(x=Stations, xend=Stations, y=0, yend=X95._percentile),

After this change, you get a nice plot: enter image description here

Upvotes: 1

Related Questions