简体   繁体   English

使用ggplot2进行水平条形图绘制

[英]Horizontal bar plot using ggplot2 and plotly

The given plot generates a barplot using ggplot2 and plotly. 给定的图使用ggplot2生成一个barplot并进行绘图。 I want to create a similar horizontal barplot using ggplotly(p). 我想使用ggplotly(p)创建一个类似的水平条形图。 Tried using the attribute coord_flip() in geom_bar() but no help. 尝试在geom_bar()中使用属性coord_flip(),但没有帮助。 Please help me and thanks. 请帮助我,谢谢。

library(plotly)
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23))
p <- ggplot(data=dat, aes(x=time, y=total_bill)) +
geom_bar(stat="identity")
p <- ggplotly(p)

If you install the development version of ggplot2, you can change the orientation to horizontal in the plot object (as described here ): 如果您安装GGPLOT2的开发版本,你可以改变方向在剧情对象为水平(如描述这里 ):

p <- ggplot(data=dat, aes(x=time, y=total_bill)) +
  geom_bar(stat="identity") +
  coord_flip()

l = plotly_build(p)
l$data[[1]]$orientation <- "h"
l

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM