简体   繁体   English

使用 ggplot2 在条形图上添加症状表?

[英]Adding symptoms table on the bars with ggplot2?

I want to add my symptom names on each bar corresponding to each colour, in ggplot2, R. I want the names on each bar to be aligned with the colour and I pressume a parameter with regards to the size of each symptom word has to be passed, yet do not know how.我想在 ggplot2、R 中与每种颜色对应的每个条形上添加我的症状名称。我希望每个条形上的名称与颜色对齐,并且我对每个症状词的大小施加一个参数必须是过去了,还不知道怎么办。

I have tried but did not succeed.我尝试过但没有成功。 This is how it looks with what I have tried:这是我尝试过的样子:

在此处输入图片说明

a fake data can be found to this link:可以在此链接中找到虚假数据:

https://github.com/gabrielburcea/stackoverflow_fake_data/blob/master/labels_symptoms_ontop_of_bar_data.csv

And this is the code I have tried:这是我试过的代码:

plot_adjusted_rates <- ggplot2::ggplot(fake_data, 
                                       ggplot2::aes(country, value)) +
  ggplot2::coord_flip() +
  ggplot2::geom_bar(ggplot2::aes(fill = symptoms), width = 0.4,
                    position = position_dodge(width = 0.5), stat = "identity") +
  geom_text(aes(label = symptoms), nudge_x = c(0.22, -0.22)) + 
  jcolors::scale_color_jcolors(palette = "pal12" )+ 
  ggplot2::labs(title = title,
                subtitle = "\nNote: Adjusted rates for symptoms in responders tested  in %",
                x = "Symptoms in /Countries", y = "Percentage", caption = "Source: Your.md Data") +
  ggplot2::theme(axis.title.y = ggplot2::element_text(margin = ggplot2::margin(t = 0, r = 21, b = 0, l = 0)),
                 plot.title = ggplot2::element_text(size = 12, face = "bold"),
                 plot.subtitle = ggplot2::element_text(size = 10),
                 legend.position = "bottom" , legend.box = "horizontal") +
  ggplot2::theme_bw()


plot_adjusted_rates

Here is a general code chunk that would solve your problem:这是一个可以解决您的问题的通用代码块:

# load librariy
library(dplyr)
library(ggplot2)

# load data
data_url = 'https://raw.githubusercontent.com/gabrielburcea/stackoverflow_fake_data/master/labels_symptoms_ontop_of_bar_data.csv'
fake_data = read.csv(data_url)

# plot
plot = ggplot(fake_data, aes(x = country, y = value, fill = symptoms)) +
  geom_bar(stat = "identity", show.legend = FALSE,
           width = 0.4, position = position_dodge(width = 0.5)) +
  coord_flip() + 
  geom_text(aes(label = symptoms), size = 3,
            hjust = -0.05, position = position_dodge2(width = 0.5))

Output:输出:

在此处输入图片说明

However, as you can see, the labels from geom_text() are overlapping.但是,如您所见, geom_text()中的标签是重叠的。 It happens because the same country and symptoms have more than one value in your fake data.发生这种情况是因为同一个国家和症状在您的虚假数据中具有多个价值。 The country A shows a particular "problem", because the same symptom have pretty different values (24.05 and 44.05).国家A显示了一个特定的“问题”,因为相同的症状具有完全不同的值(24.05 和 44.05)。 It makes me think if you shouldn't clean your data... Take a look:这让我想如果你不应该清理你的数据......看看:

dplyr::filter(fake_data, symptoms == 'loss_appetite')

Output:输出:

   country      symptoms    value
1        A loss_appetite 24.05464
2        A loss_appetite 24.05464
3        A loss_appetite 24.05464  <- 24.05
4        A loss_appetite 44.05464  <- 44.05
5        B loss_appetite 31.25430
6        B loss_appetite 31.25430
7        B loss_appetite 31.25430
8        B loss_appetite 31.25430
9        C loss_appetite 32.44539
10       C loss_appetite 32.44539
11       C loss_appetite 32.44539
12       C loss_appetite 32.44539
13       D loss_appetite 36.52090
14       D loss_appetite 36.52090
15       D loss_appetite 36.52090
16       D loss_appetite 36.52090
17       E loss_appetite 20.65789

To avoid the overlapping text issue, as well as the data "mistake" pointed out, you can use groupy() and summarise() , in order to sort out only the maximum values for each group and place the text on the top of each bar.为避免文本重叠问题以及指出的数据“错误”,您可以使用groupy()summarise() ,以便仅对每个组进行排序,并将文本放在每个组的顶部酒吧。

Thus, this code should plot the data as you wish:因此,此代码应按您的意愿绘制数据:

# create label data
fake_text = fake_data %>%
  group_by(country, symptoms) %>%
  summarize(max = max(value))

# plot
ggplot(fake_data, aes(x = country, y = value, fill = symptoms)) +
  geom_bar(stat = "identity", show.legend = FALSE,
           width = 0.4, position = position_dodge(width = 0.5)) +
  coord_flip() + 
  geom_text(aes(label = symptoms), size = 3,
            hjust = -0.05, position = position_dodge2(width = 0.5))

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM