Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 堆叠条形图中的VZ不一致_R_Ggplot2_Bar Chart - Fatal编程技术网

R 堆叠条形图中的VZ不一致

R 堆叠条形图中的VZ不一致,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我有一个堆叠的酒吧阴谋,高度高度高度不等的酒吧。我想在每个栏的顶部显示百分比 到目前为止,我所做的工作如下 df = structure(list(Type = c("Bronchoscopy", "Bronchoscopy", "Endoscopy", "Endoscopy"), Bacteremia = structure(c(1L, 2L, 1L, 2L), .Label = c("False", "True"), class = "factor"), count = c(2710L,

我有一个堆叠的酒吧阴谋,高度高度高度不等的酒吧。我想在每个栏的顶部显示百分比

到目前为止,我所做的工作如下

df = structure(list(Type = c("Bronchoscopy", "Bronchoscopy", "Endoscopy", 
"Endoscopy"), Bacteremia = structure(c(1L, 2L, 1L, 2L), .Label = c("False", 
"True"), class = "factor"), count = c(2710L, 64L, 13065L, 103L
), perc = c(97.6928622927181, 2.3071377072819, 99.2178007290401, 
0.782199270959903)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -4L), groups = structure(list(
Type = c("Bronchoscopy", "Endoscopy"), .rows = list(1:2, 
    3:4)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = TRUE))

ggplot(df, aes(x = Type, y = perc, fill = Bacteremia)) +
geom_bar(stat = "identity") +
ylab("percent") + 
geom_text(aes(label = paste0(round(perc, 2), "%")), position = 
position_stack(vjust = -0.1), color = "black", fontface = "bold") 
我似乎无法正确使用
vjust
。看起来底部和顶部条的行为方式不同。 我想实现的是将百分比略高于每个条的顶部边缘


有什么想法吗?

这里有一个可能的方法:

ggplot(df, aes(x = Type, y = perc, fill = Bacteremia)) +
  geom_bar(stat = "identity") +
  ylab("percent") + 
  geom_text(aes(label = paste0("", round(perc, 2), "%\n"), y = perc),
            color = "black", fontface = "bold", nudge_y = 2) 
我应该详细说明
ggplot2
将尝试将
geom_text()
放置在相对于数据的位置。如果您试图水平对齐文本标签,则需要使用
annotate()
或提供带有
类型
百分比
菌血症
的标签数据集,并在
geom_text()
中调用该数据集,如下所示

labdf <- cbind(df, ypos = c(103, 5, 103, 5))

ggplot(df, aes(x = Type, y = perc, fill = Bacteremia)) +
  geom_bar(stat = "identity") +
  ylab("percent") + 
  geom_text(data = labdf,
            aes(label = paste0("", round(perc, 2), "%"), y = ypos, x = Type),
            color = "black", fontface = "bold") 

labdf以下是一种可能的方法:

ggplot(df, aes(x = Type, y = perc, fill = Bacteremia)) +
  geom_bar(stat = "identity") +
  ylab("percent") + 
  geom_text(aes(label = paste0("", round(perc, 2), "%\n"), y = perc),
            color = "black", fontface = "bold", nudge_y = 2) 
我应该详细说明
ggplot2
将尝试将
geom_text()
放置在相对于数据的位置。如果您试图水平对齐文本标签,则需要使用
annotate()
或提供带有
类型
百分比
菌血症
的标签数据集,并在
geom_text()
中调用该数据集,如下所示

labdf <- cbind(df, ypos = c(103, 5, 103, 5))

ggplot(df, aes(x = Type, y = perc, fill = Bacteremia)) +
  geom_bar(stat = "identity") +
  ylab("percent") + 
  geom_text(data = labdf,
            aes(label = paste0("", round(perc, 2), "%"), y = ypos, x = Type),
            color = "black", fontface = "bold") 

labdf这里有一种方法:

df <- 
  tibble(
    Type = c("Bronchoscopy", "Bronchoscopy", "Endoscopy", "Endoscopy"),
    Bacteremia = c("False", "True", "False", "True"),
    count = c(2710L, 64L, 13065L, 103L)
  ) %>% 
  group_by(Type) %>% 
  mutate(Percent = round((count / sum(count) * 100), 1))

df %>% 
  ggplot(aes(x = Type, y = Percent, fill = Bacteremia)) +
  geom_col() +
  geom_label(
    data = . %>% filter(Bacteremia == "True"), 
    aes(y = Percent + 5, label = str_c(Percent, "%")),
    show.legend = FALSE
  ) + 
  geom_label(
    data = . %>% filter(Bacteremia == "False"), 
    aes(y = 105, label = str_c(Percent, "%")),
    show.legend = FALSE
  )
df%
分组依据(类型)%>%
变异(百分比=四舍五入((计数/总和(计数)*100),1))
df%>%
ggplot(不良事件(x=类型,y=百分比,填充=菌血症))+
geom_col()+
geom_标签(
数据=.%>%过滤器(菌血症=“真”),
不良事件(y=百分比+5,标签=str_c(百分比,“%”),
show.legend=FALSE
) + 
geom_标签(
数据=.%>%过滤器(菌血症=“假”),
不良事件(y=105,标签=str_c(百分比,“%”),
show.legend=FALSE
)
选择5和105在我的电脑上工作,但可能需要根据您的具体设置和纵横比进行一些调整。第一个
geom_label
调用根据精确的百分比设置y轴,而第二个调用将其设置为高于条形的恒定水平


您可能还想尝试使用
geom_text
geom_label
来试验不同的颜色和标签设置。
geom_label
的好处在于,它可以非常清楚地显示要标记的组。

以下是一种方法:

df <- 
  tibble(
    Type = c("Bronchoscopy", "Bronchoscopy", "Endoscopy", "Endoscopy"),
    Bacteremia = c("False", "True", "False", "True"),
    count = c(2710L, 64L, 13065L, 103L)
  ) %>% 
  group_by(Type) %>% 
  mutate(Percent = round((count / sum(count) * 100), 1))

df %>% 
  ggplot(aes(x = Type, y = Percent, fill = Bacteremia)) +
  geom_col() +
  geom_label(
    data = . %>% filter(Bacteremia == "True"), 
    aes(y = Percent + 5, label = str_c(Percent, "%")),
    show.legend = FALSE
  ) + 
  geom_label(
    data = . %>% filter(Bacteremia == "False"), 
    aes(y = 105, label = str_c(Percent, "%")),
    show.legend = FALSE
  )
df%
分组依据(类型)%>%
变异(百分比=四舍五入((计数/总和(计数)*100),1))
df%>%
ggplot(不良事件(x=类型,y=百分比,填充=菌血症))+
geom_col()+
geom_标签(
数据=.%>%过滤器(菌血症=“真”),
不良事件(y=百分比+5,标签=str_c(百分比,“%”),
show.legend=FALSE
) + 
geom_标签(
数据=.%>%过滤器(菌血症=“假”),
不良事件(y=105,标签=str_c(百分比,“%”),
show.legend=FALSE
)
选择5和105在我的电脑上工作,但可能需要根据您的具体设置和纵横比进行一些调整。第一个
geom_label
调用根据精确的百分比设置y轴,而第二个调用将其设置为高于条形的恒定水平


您可能还想尝试使用
geom_text
geom_label
来试验不同的颜色和标签设置。
geom_label
的好处在于,它可以非常清楚地显示要标记的是哪个组。

如果百分比高于每个条,那么这是否意味着对于底部堆栈,它们看起来可能一眼就指的是上面的堆栈,因为它们将位于上部堆栈上?我想这就是为什么它不是一个明显的选择。我个人会使用
vjust=0.5
。如果百分比高于每个条,那么这是否意味着对于底部堆栈,一眼就可以看出它们是指上面的堆栈,因为它们将位于上部堆栈上?我想这就是为什么它不是一个明显的选择。我个人会使用
vjust=0.5