在用R的ggplot2包的barplot函数绘制条形图时,图例部分的范围数字数字很长很难看,请问有什么方法可以把有效位数缩减为2或3位么?
您可以使用ggplot2中的scales包来控制条形图中图例的范围数字的显示。具体来说,您可以使用scale_y_continuous()或scale_x_continuous()函数来设置y轴或x轴上的比例尺,并使用round()函数来指定数字舍入的位数。
参考:
geom_bar参数.
geom_bar(mapping = NULL, data = NULL, stat = “count”, width=0.9, position=“stack”)
position_stack(vjust = 1, reverse = FALSE)
position_dodge(width = NULL)
position_fill(vjust = 1, reverse = FALSE)
data(Arthritis)
library(vcd)
library(ggplot2)
data("Arthritis")
ggplot(data=ToothGrowth, mapping=aes(x=dose))+
geom_bar(stat="count")
#-------------
mytable <- with(Arthritis,table(Improved))
df <- as.data.frame(mytable)
ggplot(data=df, mapping=aes(x=Improved,y=Freq))+
geom_bar(stat="identity")
#--------------
ggplot(data=Arthritis,aes(x=Improved))+
geom_bar(stat="count",width=0.5, color='red',fill='steelblue')
#--------------
ggplot(data=Arthritis, mapping=aes(x=Improved))+
geom_bar(stat="count",width=0.5, color='red',fill='steelblue')+
geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5)+
theme_minimal() #aes(label=..count..)表示显示变量值
#---------------
mytable <- with(Arthritis,table(Improved))
df <- as.data.frame(mytable)
ggplot(data=df, mapping=aes(x=Improved,y=Freq))+
geom_bar(stat="identity",width=0.5, color='red',fill='steelblue')+
geom_text(aes(label=Freq), vjust=1.6, color="white", size=3.5)+
theme_minimal()
#----------------------
#color 和 fill 可更改的颜色参数,一般柱状图,fill是柱子的填充颜色,
#这时就使用scale_fill系列函数来更改颜色。点图和线使用color分组,
#则使用scale_color_系列函数来更改颜色。
ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Improved))+
geom_bar(stat="count",width=0.5)+
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
#scale_color_manual(values=c("56B4E9", "#56B4E9", "#56B4E9"))+
geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5)+
theme_minimal()
#-----------------
#图例
p <- ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Improved))+
geom_bar(stat="count",width=0.5)+
scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5)+
theme_minimal()
p + theme(legend.position="top")
p + theme(legend.position="bottom")
# Remove legend
p + theme(legend.position="none")
#--------------
#修改条形图顺序
p <- ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Improved))+
geom_bar(stat="count",width=0.5)+
scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5)+
theme_minimal()
p + scale_x_discrete(limits=c("Marked","Some", "None"))
#-----------------
#堆叠
ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Sex))+
geom_bar(stat="count",width=0.5,position='stack')+
scale_fill_manual(values=c('#999999','#E69F00'))+
geom_text(stat='count',aes(label=..count..), color="white", size=3.5,position=position_stack(0.5))+
theme_minimal()
#并行
y_max <- max(aggregate(ID~Improved+Sex,data=Arthritis,length)$ID)
#aggregate(Arthritis$ID,list(Arthritis$Improved,Arthritis$Sex),length)
p <- ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Sex))+
geom_bar(stat="count",width=0.5,position='dodge')+
scale_fill_manual(values=c('#999999','#E69F00'))+
ylim(0,y_max+5)+
geom_text(stat='count',aes(label=..count..), color="black", size=3.5,position=position_dodge(0.5),vjust=-0.5)+
theme_minimal()
#添加文本的函数用的时候可以再了解
#annotate() 位置和文本字体大小角度可以随意设置
p+annotate("text",x=3,y=30,label="冲鸭~~",size=11,angle=30,alpha=0.2)#+
#annotate("text",x=7,y=2.2,label="跟着菜鸟一起学R语言",size=8,angle=-15,alpha=0.9,colour="red",family="JP3")+
# annotate("text",x=7,y=4,label="跟着菜鸟一起学R语言",size=6,angle=-30,alpha=0.5,colour="blue",family="JP4")+
# annotate("text",x=5,y=4,label="跟着菜鸟一起学R语言",size=8,angle=-10,alpha=0.7,colour="green",family="JP1")+
# annotate("text",x=5,y=3,label="跟着菜鸟一起学R语言",size=6,angle=-30,alpha=0.5,colour="blue",family="JP5")
对于这个问题,可以使用ggplot2包中的scale_y_continuous函数来设置y轴的有效位数,具体步骤如下:
ggplot(data, aes(x, y, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(0.9)) +
scale_fill_manual("Legend", values = c("#E2E2E2", "#4C688B", "#EAB43F")) +
xlab("X Label") +
ylab("Y Label")
ggplot(data, aes(x, y, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(0.9)) +
scale_fill_manual("Legend", values = c("#E2E2E2", "#4C688B", "#EAB43F")) +
xlab("X Label") +
ylab("Y Label") +
scale_y_continuous(limits = c(0, 1000), format = ",.2f")
在这个示例中,我们将有效位数设置为2,通过format = ",.2f"来实现。
如果想要调整刻度线的数字间隔,可以使用ggplot2中的breaks参数,如:
scale_y_continuous(limits = c(0, 1000), format = ",.2f", breaks = seq(0, 1000, by = 200))
在上面的示例中,我们将刻度线间隔设置为200。