在R中绘制图表时的问题(Dodge或Geom_line)

发布于 2025-01-26 15:39:51 字数 215 浏览 2 评论 0 原文

我想在每组(t)的图中表达每列(a,b,c,d,e)的频率。 我不知道该怎么办。 如图所示,数据是以数据框架的形式选择的,但是我不知道在下图中表达该数据。 我尝试仅提取使用GGPLOT的值,但即使这也没有解决。有一个好方法吗? 有没有办法用钢筋图以外的线表达它? 帮我。 “在此处输入图像说明”

I want to express the frequency of each column (a,b,c,d,e) in a graph of (t) for each group.
I don't know what to do.
As shown in the figure, the data was selected in the form of a data frame, but I don't know what to do when I express it in the next graph.
I tried to extract only the value for a with ggplot, but even this didn't work out. Is there a good way?
Is there a way to express it with lines other than bar graphs?
Help me.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2025-02-02 15:39:51

您可以使用以下代码:

df <- data.frame(t = c("m", "i", "j", "z"),
                 a = c(300, 20, 15, 500),
                 b = c(100, 44, 99, 320),
                 c = c(200, 57, 98, 17),
                 d = c(150, 52, 122, 666),
                 e = c(50, 89, 64, 68))

library(tidyverse)
library(reshape)
df %>%
  melt(id.vars = "t") %>%
  ggplot(aes(x = t, y = value, fill = variable)) +
  geom_col(position = "dodge") 

输出:

”在此处输入图像描述”

You can use the following code:

df <- data.frame(t = c("m", "i", "j", "z"),
                 a = c(300, 20, 15, 500),
                 b = c(100, 44, 99, 320),
                 c = c(200, 57, 98, 17),
                 d = c(150, 52, 122, 666),
                 e = c(50, 89, 64, 68))

library(tidyverse)
library(reshape)
df %>%
  melt(id.vars = "t") %>%
  ggplot(aes(x = t, y = value, fill = variable)) +
  geom_col(position = "dodge") 

Output:

enter image description here

掐死时间 2025-02-02 15:39:51

将数据旋转更长以合并列A到e的所有计数,仅在一个名称和值列内,可以用作GGPLOT的维度美学:

library(tidyverse)

data <- tribble(
  ~t, ~a, ~b,
  "m", 300, 100,
  "i", 20, 44,
  "j", 15, 99
)

data %>%
  pivot_longer(-t) %>%
  ggplot(aes(t, value, fill = name)) +
  geom_col(position = "dodge")

“”

Pivot the data longer to merge all counts of columns a to e inside just one name and value column, which can be used as dimension aesthetics for ggplot:

library(tidyverse)

data <- tribble(
  ~t, ~a, ~b,
  "m", 300, 100,
  "i", 20, 44,
  "j", 15, 99
)

data %>%
  pivot_longer(-t) %>%
  ggplot(aes(t, value, fill = name)) +
  geom_col(position = "dodge")

Created on 2022-05-06 by the reprex package (v2.0.0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文