【发布时间】:2021-12-14 03:40:39
【问题描述】:
我正在使用 ggplot2 在一个图中绘制多个向量。 我有以下数据框
| Array | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Arr1 | 0.1 | 0.1 | 0.1 | 0.2 | 0.2 | 0.2 | 0.7 | 0.7 | 0.4 | 0.7 |
| Arr2 | 0.6 | 0.6 | 0.6 | 0.1 | 0.1 | 0.1 | 0.1 | 0.1 | 0.5 | 0.1 |
| Arr3 | 0.3 | 0.3 | 0.3 | 0.7 | 0.7 | 0.7 | 0.2 | 0.2 | 0.1 | 0.2 |
| Arr4 | 0.4 | 0.6 | 0.7 | 0.2 | 0.1 | 0.3 | 0.4 | 0.5 | 0.3 | 0.9 |
| B | a | a | a | b | b | b | a | a | a | b |
| C | b | b | b | a | a | b | b | a | b | a |
所以我把数据融合起来,用水平堆栈栏绘制所有向量,如下:
df2<-df %>%
melt(id.vars = "Array") %>%
mutate(variable = str_extract(variable, "[0-9]+")) %>%
mutate(value = case_when(
value == "a" ~ 1,
value == "b" ~ 2,
TRUE ~ as.numeric(value)
)) %>%
mutate(variable = as.numeric(variable))
df2 %>%
ggplot(aes(x = Array, y = variable, group = Array, fill = value)) +
geom_col() + coord_flip()
但是x轴不正确,图像显示向量B中相同数量的a和b具有不同的大小,最后一个元素的大小也比前三个大。 x 轴上的问题用矢量B and C 比Arr 更容易检测。
当您查看 df2 variable 只有 1 到 10 时,我无法弄清楚 x 轴上的点数如何超过 50。
【问题讨论】:
-
我无法复制此内容。请将 dput(df) 的结果添加到您的问题中。另外请显示您正在使用哪些软件包
library(tidyverse)library(reshape2)? -
是的
library(tidyverse) and library(reshape2)被使用