在用R的ggplot2包的barplot函数绘制条形图时,图例部分的范围数字数字很长很难看,请问有什么方法可以把有效位数缩减为2或3位么

在用R的ggplot2包的barplot函数绘制条形图时,图例部分的范围数字数字很长很难看,请问有什么方法可以把有效位数缩减为2或3位么?

img


更改为

img

您可以使用ggplot2中的scales包来控制条形图中图例的范围数字的显示。具体来说,您可以使用scale_y_continuous()或scale_x_continuous()函数来设置y轴或x轴上的比例尺,并使用round()函数来指定数字舍入的位数。

  • 文章:R语言ggplot2可视化散点图实战:绘制基础散点图、为所有散点添加标签、只为大于阈值的散点添加标签 中也许有你想要的答案,请看下吧
  • 除此之外, 这篇博客: ggplot2分组柱图barplot添加误差线以及显著标记中的 关于bar堆叠的问题,一些参数解释 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 参考:
    geom_bar参数.

    geom_bar(mapping = NULL, data = NULL, stat = “count”, width=0.9, position=“stack”)

    • stat:设置统计方法,有效值是count(默认值) 和 identity,其中,count表示条形的高度是变量的数量,identity表示条形的高度是变量的值;
    • position:位置调整,有效值是stack、dodge和fill,默认值是stack(堆叠),是指两个条形图堆叠摆放,dodge是指两个条形图并行摆放,fill是指按照比例来堆叠条形图,每个条形图的高度都相等,但是高度表示的数量是不尽相同的。
    • width:条形图的宽度,是个比值,默认值是0.9
    • color:条形图的线条颜色
    • fill:条形图的填充色
    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")
    
    
    

    在这里插入图片描述

    在这里插入图片描述

  • 以下回答由chatgpt基于相关博客总结生成:

    对于这个问题,可以使用ggplot2包中的scale_y_continuous函数来设置y轴的有效位数,具体步骤如下:

    1. 首先,根据参考资料中的段落4,使用ggplot函数创建条形图,并为y轴设置标签和x轴标签;
    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")
    
    1. 然后,在ggplot函数后添加scale_y_continuous函数,通过limits参数来设置y轴的上下限,并使用format参数来控制有效位数;
    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"来实现。

    1. 最后,运行上述代码即可生成缩减有效位数的条形图。

    如果想要调整刻度线的数字间隔,可以使用ggplot2中的breaks参数,如:

    scale_y_continuous(limits = c(0, 1000), format = ",.2f", breaks = seq(0, 1000, by = 200))
    

    在上面的示例中,我们将刻度线间隔设置为200。