根据R中的范围和绘制一个堆积条形图 [英] Making a stacked bar plot based on ranges in R and plotly

查看:213
本文介绍了根据R中的范围和绘制一个堆积条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中创建一个堆积条形图并绘制使用虹膜数据集。在x轴上,我想在代码中设置下面的iris_limits限制,并且y轴应该包含适合这些范围的所有Sepal.Length值。我想将这些值作为单个向量传递。另外,如果通过理解Sepal.Length的范围而不是硬编码来限制动态范围,请提供帮助。我写了一个带有值的基本脚本来给你一个想法。

I want to create a stacked bar chart in R and plotly using iris dataset. In the x-axis, I want to set limits like iris_limits below in the code and the y-axis should contain all the Sepal.Length values which fit into these ranges. I want to pass the values as a single vector. Also, if the limits can be made dynamic by understanding the range of the Sepal.Length instead of hard coding it, please help. I have written a basic script with values to give you an idea. Thanks.

library(plotly)
iris_limits <- c("1-4", "4-6", "6-8")
sepal <- c(2.4,5.4,7.1)
data <- data.frame(iris_limits, sepal)
p <- plot_ly(data, x = ~iris_limits, y = ~sepal, type = 'bar', name = 
'Sepal') %>%
layout(yaxis = list(title = 'Count'), barmode = 'group')
p


推荐答案

最好理解。首先将萼片长度分为所需的类别 iris_limits :1-3,3-6,6-9

I tried my best to understand. First dividing the sepal length to the desired categories iris_limits: "1-3","3-6","6-9"

iris$iris_limits <- cut(iris$Sepal.Length, c(1,3,6,9))

注意:没有sepal长度在1-3之间,所以您只有2个组。

Note: no sepal length is in between 1-3, so you only have 2 groups.

然后你想每个萼片长度限制作为x轴上的一个单独的栏,并且每个单独的sepal长度落入类别中以便彼此堆叠在一起?您链接到堆叠条形图,其中堆叠条的颜色不同,这是您想要的吗?

Then you want each sepal length limit as a separate bar on the x axis, and each individual sepal length falling into category to be bar stacked onto each other? You linked to a stack bar chart with varying color for the stacked bars, is this what you want?

为每个sepal长度创建一个ID:

Create an ID for each sepal length:

iris$ID <- factor(1:nrow(iris))

绘制,设置 color =〜ID 如果您想为堆积条纹指定不同的颜色:

Plot, set color=~ID if you want different colors for the stacked bars:

library(plotly)
p <- plot_ly(iris, x = ~iris_limits, y = ~Sepal.Length, type = 'bar', color=~ID) %>%
  layout(yaxis = list(title = 'Count'), barmode = 'stack')

EDITED 按照 iris_limits 进行分组,但我切换到 ggplot2 以利用 facet_wrap 按照 iris_limits 分隔的功能,然后使用 gg绘图

EDITED For version that is not stacked but grouped by iris_limits, I switched to ggplot2 to make use of facet_wrap functionality to segregate by iris_limits, then use ggplotly.

gg <- ggplot(iris, aes(x=ID, y=Sepal.Length, fill=iris_limits)) + 
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~iris_limits, scales="free_x", labeller=label_both) +
  theme_minimal() + xlab("") + ylab("Sepal Length") +
  theme(axis.text.x=element_blank())
ggplotly(gg)

编辑:回复:更改图例标题和工具提示显示

要更改图例标题,请使用 labs 。在这里,还有必要将 theme 下的 legend.title 字体大小更改为适合 ggplotly margin。

To change the legend title, use labs. Here it was also necessary to change the legend.title font size under theme to fit the ggplotly margins.

要更改工具提示文本,请将 text 参数添加到 aes 来创建所需的字符串,然后定义 aes 值显示在 tooltip ggplotly

To change the tooltip text, add text parameter to aes to create desired character string, then define aes values to be displayed in tooltip in ggplotly.

gg <- ggplot(iris, aes(x=ID, y=Sepal.Length, fill=iris_limits,
                   text=paste("Sepal Length:", Sepal.Length, "cm"))) + 
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~iris_limits, scales="free_x") +
  theme_minimal() + xlab("") + ylab("Sepal Length (cm)") +
  theme(axis.text.x=element_blank(), legend.title=element_text(size=10)) +
  labs(fill="Sepal \nLength (cm)")
ggplotly(gg, tooltip=c("x", "text"))

这篇关于根据R中的范围和绘制一个堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆