我想用ggplot制作一个显示yaxis百分比的图表.
这是我使用的数据和代码
data<-"yrs.1.17 yrs.18.44 yrs.45.64 yrs.65.84 yrs.85.
1 4.53 35.12 32.93 22.86 4.57
2 4.01 34.74 34.19 22.71 4.34
3 4.75 33.23 35.19 22.28 4.51
4 4.60 34.04 36.93 20.70 3.56
5 4.80 33.82 37.69 19.97 3.52
6 4.30 35.09 37.08 19.83 3.65
7 3.65 38.08 36.19 19.18 2.85
8 3.72 38.11 36.10 19.22 2.86"
mydata<- read.table(text=data, header=TRUE)
year<-c(2006,2007,2008,2009,2010,2011,2012,2013)
df<-data.frame(year,mydata)
library(ggplot2)
library(reshape2)
library(scales)
newdf<-melt(df,'year')
ggplot(newdf,aes(x=year,y=value,group=variable,color=variable))+ geom_line(size=1)+
scale_x_continuous(breaks=year)+xlab("Year")+scale_y_continuous(labels = percent,limits=c(1,50),breaks=seq(5,50,by=5))+ylab("Age groups")+
geom_point(aes(shape=variable),size=3)+
ggtitle("Age groups of people between 2006 and 2013")+
theme(legend.title=element_blank(), legend.justification=c(0.6,0.6), legend.position=c(0.95,0.95), legend.text = element_text(size=9),
axis.text = element_text(size=9), axis.title = element_text(size=9), plot.title=element_text(size = 9))
正如您在输出中看到的那样,百分比都乘以100.有没有办法获得正确的百分比.
非常感谢.
最佳答案 只差100:
ggplot(newdf,
aes(
x = year,
y = value / 100, # here divide by 100
group = variable,
color = variable
)
) +
geom_line(size=1)+
scale_x_continuous(breaks=year) +
xlab("Year") +
scale_y_continuous(
labels = percent,
limits = c(.01, .5), # here divide by 100
breaks = seq(.05, .5, by = .05) # here divide by 100
) +
ylab("Age groups") +
geom_point(aes(shape = variable), size=3) +
ggtitle("Age groups of people between 2006 and 2013") +
theme(legend.title=element_blank(),
legend.justification = c(0.6, 0.6),
legend.position = c(0.95, 0.95),
legend.text = element_text(size = 9),
axis.text = element_text(size = 9),
axis.title = element_text(size = 9),
plot.title = element_text(size = 9))
作为旁注,而不是element_text(size = 9)的所有手动设置,大多数主题采用base_size参数,因此使用theme_grey(9)或theme_classic(9)将为您完成大部分工作.您可能仍需要调整标题.