如何在 R 中创建带有顶部数值的 plotly 条形图?


要创建带有顶部栏上值的 plotly 条形图,我们可以遵循以下步骤 −

  • 首先,创建一个向量来表示类别,一个向量表示计数,并使用 plotly 创建条形图。
  • 然后,使用 plotly 包的 add_text 函数创建相同的条形图。

创建矢量和 plotly 条形图

使用 c 函数创建矢量和 plotly 函数,并使用 layout 函数创建带有轴标题的条形图 −

x<-c("A","B","C")
freq<-c(48,40,42)
library(plotly)
plot_ly(x=x,y=freq,type="bar")%>%layout(xaxis=list(title="X"),yaxis=list(title="Y"))

输出

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

创建在条形图顶部显示计数的条形图

使用 add_text 函数创建在条形图顶部显示计数的条形图 −

x<-c("A","B","C")
freq<-c(48,40,42)
library(plotly)

plot_ly(x=x,y=freq,type="bar")%>%layout(xaxis=list(title="X"),yaxis=list(title="Y"))%
>%add_text(x=x,y=freq,text=freq,textposition="top")

输出

更新日期: 2021-08-13

894 次浏览

开启您的职业生涯

完成课程并获得认证

开始
广告