how to add % sign to ggplot

0

Issue

I’m having trouble adding the % sign to a ggplot (R) (to the line chart)

here is my plot code:

ggplot(table)  + 
    geom_bar(aes(x=year, y=order_cnt_in_thousends),stat="identity", fill="tan1", colour="sienna3")+
    geom_line(aes(x=year, y=yearly_change ),stat="identity")+
    geom_text(aes(label=yearly_change, x=year, y=yearly_change), colour="black")+
    geom_text(aes(label=order_cnt_in_thousends, x=year, y=order_cnt_in_thousends), colour="black", vjust = -0.5) +   scale_x_date(date_breaks = "1 year")

Iv’e tried to add this to the column:
table$yearly_change <-paste(year_growth_table$yearly_change, "%") but now I have problem since it’s discrete and not continuous.

plot image

Solution

You can use the paste function inside the aes() to make it a string with "%" :

set.seed(1234)
df = data.frame(year=as.Date(c("2016-01-01","2017-01-01","2018-01-01")),order_cnt_in_thousends = sample(1000,3),yearly_change = sample(1000,3))

ggplot(df)  + 
geom_bar(aes(x=year, y=order_cnt_in_thousends),stat="identity", fill="tan1", colour="sienna3")+
geom_line(aes(x=year, y=yearly_change ))+
geom_text(aes(label=paste(yearly_change,"%"), x=year, y=yearly_change), colour="black")+
geom_text(aes(label=order_cnt_in_thousends, x=year, y=order_cnt_in_thousends), colour="black", vjust = -0.5) +   scale_x_date(date_breaks = "1 year")

enter image description here

Answered By – StupidWolf

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More