我正在尝试在ggplot2中的两个单独的堆叠条形图(相同的图形)之间绘制线条,以显示第二个条形图的两个部分是第一个条形图的子集.
我曾经尝试都geom_line和geom_segment.但是,我遇到了同样的问题geom,即在同一个图中为每个(需要两行)指定一个单独的开始和停止,而不是有五行的数据帧.
没有线的图的示例代码:
library(data.table)
Example <- data.table(X_Axis = c('Count', 'Count', 'Dollars', 'Dollars', 'Dollars'),
Stack_Group = c('Purely A', 'A & B', 'Purely A Dollars', 'B Mixed Dollars', 'A Mixed dollars'),
Value = c(10,3, 120000, 100000, 50000))
Example[, Percent := Value/sum(Value), by = X_Axis]
ggplot(Example, aes(x = X_Axis, y = Percent, fill = factor(Stack_Group))) +
geom_bar(stat = 'identity', width = 0.5) +
scale_y_continuous(labels = scales::percent)
Run Code Online (Sandbox Code Playgroud)
我正在对一些患有某种疾病的患者进行一项研究,并在 3 个不同的时间点使用顺序量表评估功能状态。我想在这些时间点的堆叠条形图中连接多个组。
我查看了这些主题,但尚未使用这些建议使其发挥作用:
有没有一种有效的方法可以使用 ggplot2 在堆积条形图中的不同元素之间绘制线条?
请查看我最终希望该图如何从 R(在 PRISM 中生成)中看到三个时间点上这 6 个序数值中每一个的频率的图形表示(顶部组没有序数值为 3、5、6 的患者) ):
使用 PRISM 的预期图形

数据:
library(tidyverse)
mrs <-tibble(
Score = c(0,1,2,3,4,5,6),
pMRS = c(17, 2, 1, 0, 1, 0, 0),
dMRS = c(2, 3, 2, 6, 4, 2, 2),
fMRS = c(4, 4, 5, 4, 1, 1, 2)
Run Code Online (Sandbox Code Playgroud)
geom_line这是我到目前为止在使用或遇到问题之前尝试过的代码geom_segment(省略这些行,因为它只是扭曲了当前的数字)
mrs <- mrs %>% mutate(across(-Score,~paste(round(prop.table(.) * 100, 2)))) %>%
pivot_longer(cols = c("pMRS", "dMRS", "fMRS"), names_to = "timepoint") %>%
mutate(Score=as.character(Score),
value=as.numeric(value)) %>% …Run Code Online (Sandbox Code Playgroud) 在 ggplot2 中,我正在尝试一件简单的事情,但由于某种原因我无法做到。我已经在数据框中调整了均值和 SE,想要绘制均值、误差线,然后将均值与点连接起来。这是代码和错误(除了将手段连接到geom_line(使用RCookbook:
library(ggplot2)
#data set
data1 <- structure(list(group = structure(1:3, .Label = c("1", "2", "3"
), class = "factor"), estimate = c(55.7466654122763, 65.0480954172939,
61.9552391704298), SE = c(2.33944612149257, 2.33243565412438,
2.33754952927041), t.ratio = c(23.8290016171476, 27.8884844271143,
26.5043535525714)), .Names = c("group", "estimate", "SE", "t.ratio"
), row.names = c(NA, 3L), class = "data.frame")
#the attempted plot
pd <- position_dodge(.1)
ggplot(data1, aes(x=group, y=estimate, group=group)) +
geom_errorbar(aes(ymin=estimate-SE, ymax=estimate+SE),
colour="black", width=.1, position=pd) +
geom_line(data=data1, aes(x=group, y=estimate)) +
geom_point(position=pd, size=4)
Run Code Online (Sandbox Code Playgroud)
错误:
ymax not …Run Code Online (Sandbox Code Playgroud)