ggplot显示所有Y轴值而不是校准值

时间:2018-04-25 00:13:57

标签: r dataframe plot ggplot2

我使用并排条绘制了几个变量的ggplot,但似乎显示的y轴没有任何校准 - 显示每个数字(%) police_kills

情节的主题是显示白人,西班牙裔和黑人(%)在美国各州的总人口中的比例。正如您所看到的,应该表示百分比的Y轴看起来像是已经被推入其中的所有值而不是0到100之间的校准

我正在使用的数据集显示在github_fivethirtyeight_police-killings(很抱歉,但我无法找到一种方法来组织我从数据框中获取的五个列:州,种族和三个份额你在右边看到(%)

显示R代码:
x<-read.csv("C:/Users/USER/data/police-killings/police_killings.csv",header=TRUE, sep = "," ,stringsAsFactors = FALSE) state<-x[,10] ethnicity<-x[,4] state_and_shares<-x[,c(10,23:25)]

df2<-melt(state_and_shares, id.vars = 'state') head(df2)

ggplot(df2,aes(x=state,y=value,fill=variable))+geom_bar(stat = 'identity',position = 'dodge')+theme(axis.text.x = element_text(angle = 90, hjust = 1))

有人可以告诉我如何将Y值看作更正常并且不显示所有值?

1 个答案:

答案 0 :(得分:1)

你需要ggplot将y值看作数字而不是字符串,例如。使用as.numeric():

ggplot(df2,aes(x=state,y=as.numeric(value),fill=variable))+geom_bar(stat = 'identity',position = 'dodge')+theme(axis.text.x = element_text(angle = 90, hjust = 1))

enter image description here