简体   繁体   English

将文本标签添加到ggplot2镶嵌图中

[英]Add text labels to a ggplot2 mosaic plot

Using the following data: 使用以下数据:

Category <- c("Bankpass", "Bankpass", "Bankpass", "Moving", "Moving")
Subcategory <- c("Stolen", "Lost", "Login", "Address", "New contract")
Weight <- c(10,20,13,40,20)
Duration <- as.character(c(0.2,0.4,0.5,0.44,0.66))
Silence <- as.character(c(0.1,0.3,0.25,0.74,0.26))
df <- data.frame(Category, Subcategory, Weight, Duration, Silence)

Which I use to create the following mosaic plot: 我用它来创建以下马赛克图:

library (ggplot2)
library (ggmosaic)

g <- ggplot(data = df) +
  geom_mosaic(aes(weight = Weight, x = product(Category), fill = Duration), 
              offset = 0, na.rm = TRUE) +  
  theme(axis.text.x = element_text(angle = -25, hjust = .1)) +
  theme(axis.title.x = element_blank()) +
  scale_fill_manual(values = c("#e8f5e9", "#c8e6c9", "#a5d6a7", "#81c784", "#66bb6a"))

在此输入图像描述

This works, however I would like to include text labels on the elements on the graph ("Showing fe stolen, lost" etc.) 这是有效的,但我想在图表上的元素上包含文字标签(“显示fe被盗,丢失”等)

However, when I do: 但是,当我这样做时:

g + geom_text(x = Category, y = Subcategory, label = Weight)

I get the following error: 我收到以下错误:

Error in UseMethod("rescale") : no applicable method for 'rescale' applied to an object of class "character" UseMethod中的错误(“rescale”):没有适用于“rescale”的方法应用于类“character”的对象

Any thoughts on what goes wrong here? 对这里出了什么问题的想法?

Here is my attempt. 这是我的尝试。 The x-axis is in a discrete variable (ie, Category). x轴是离散变量(即,类别)。 So you cannot use it in geom_text() . 所以你不能在geom_text()使用它。 You somehow need to create a numeric variable for the axis. 你不知何故需要为轴创建一个数字变量。 Similarly, you need to find position in the y-axis for labels. 同样,您需要在y轴上找到标签的位置。 In order to get numeric values for the two dimensions, I decided to access to the data frame staying behind your graphic. 为了获得两个维度的数值,我决定访问保留在图形后面的数据框。 When you use the ggmosaic package, there is one data frame behind a graphic in this case. 当您使用ggmosaic包时,在这种情况下,图形后面有一个数据框。 You can get it using ggplot_build() . 你可以使用ggplot_build()来获取它。 You can calculate x and y values using the information in the data frame (eg, xmin, and xmax). 您可以使用数据框中的信息(例如,xmin和xmax)计算x和y值。 This is good news. 这是个好消息。 But, we have bad news too. 但是,我们也有坏消息。 When you reach the data, you realize that there is no information about Subcategory that you need for labels. 当您到达数据时,您意识到标签不需要有关子类别的信息。

We can overcome this challenge joining the data frame above with the original data. 我们可以克服这一挑战,将上述数据框与原始数据相结合。 When I joined the data, I calculated proportion for both the original data and the other data. 当我加入数据时,我计算了原始数据和其他数据的比例。 The values are purposely converted to character. 这些值有意转换为字符。 temp is the data set you need in order to add labels. temp是添加标签所需的数据集。

library(dplyr)
library(ggplot2)
library(ggmosaic)

# Add proportion for each and convert to character for join

df <- group_by(df, Category) %>%
      mutate(prop = as.character(round(Weight / sum(Weight),3)))

# Add proportion for each and convert to character.
# Get x and y values for positions
# Use prop for join

temp <- ggplot_build(g)$data %>%
        as.data.frame %>%
        transmute(prop = as.character(round(ymax - ymin, 3)),
                  x.position = (xmax + xmin) / 2,
                  y.position = (ymax + ymin) / 2) %>%
        right_join(df)

g + geom_text(x = temp$x.position, y = temp$y.position, label = temp$Subcategory) 

在此输入图像描述

I think you are looking for something like this 我想你正在寻找这样的东西

library(ggplot2)
library(ggmosaic)

Your data: 你的数据:

Category <- c("Bankpass", "Bankpass", "Bankpass", "Moving", "Moving")
Subcategory <- c("Stolen", "Lost", "Login", "Address", "New contract")
Weight <- c(10,20,13,40,20)
Duration <- as.character(c(0.2,0.4,0.5,0.44,0.66))
Silence <- as.character(c(0.1,0.3,0.25,0.74,0.26))
mydf <- data.frame(Category, Subcategory, Weight, Duration, Silence)

ggplot(data = mydf) +
    geom_mosaic(aes( x = product(Duration, Subcategory), fill=factor(Duration)), na.rm=TRUE) + 
    theme(axis.text.x=element_text(angle=-25, hjust= .1)) +
    labs(x="Subcategory", title='f(Duration, Subcategory | Category)')  + 
    facet_grid(Category~.) + 
    guides(fill=guide_legend(title = "Duration", reverse = TRUE))

The output is: 输出是:

在此输入图像描述

It is almost the best you can do on ggmosaic package. 它几乎是你在ggmosaic包上做的最好的。 You should try other packages. 你应该尝试其他包。

Good luck for your project work ;-) 祝你的项目工作顺利;-)

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

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