ggplot2 bar chart labels and colours

问题内容:

I’m having an issue with adding labels and colours to a bar chart using geom_text.

Here’s an eg of the data: Data

For labels

The labels bunch up together, rather than there being a figure at the end of each bar. (chart example)

ggplot(data = a, aes(x = Answer, y = freq)) + 
geom_bar(aes(fill = season),stat = "identity", position = "dodge") +
theme_minimal() + 
scale_y_continuous(labels=scales::percent,limits= c(0, 1))+
geom_text(aes(label=freq,group=Answer),position=position_dodge    
(width=0.5),vjust=-1.5) +
ggtitle(label = "x")+
labs (x="%")+
coord_flip()

I would like there to be a proportion at the end of each bar, rather than them overlapping on each other.

I would also like the propotions to show as *100. So 77.0%, rather than 0.77

For colours

I would like to amend the colours from the standard blue and red here as well. When I add a palette with four colours each bar gets an individual colour (chart example 2), rather than one for ‘spring’ and one for ‘winter’. You’ll see that doing this also messes up all the labels and the legend.

If I use a colour palette with two colours I get this:

‘Error: Aesthetics must be either length 1 or the same as the data (4): fill, x, y’

ggplot(data = a, aes(x = Answer, y = freq)) + 
geom_bar(aes(fill = "palette"),stat = "identity", position = "dodge") +
theme_minimal() + 
scale_y_continuous(labels=scales::percent,limits= c(0, 1))+
geom_text(aes(label=freq,group=Answer),position=position_dodge  
(width=0.5),vjust=-1.5) +
ggtitle(label = "x")+
labs (x="%")+
coord_flip()

Thanks in advance for any help!

问题评论:

    
please post the output of dput instead of using external links for providing your data.

原文地址:

https://stackoverflow.com/questions/47756720/ggplot2-bar-chart-labels-and-colours

添加评论

友情链接:蝴蝶教程