简体   繁体   English

使用y轴值在ggplot2中创建辅助x轴

[英]using y-axis values to create secondary x-axis in ggplot2

I would like to create a dot plot with percentiles, which looks something like this- 我想创建一个带百分位数的点图,看起来像这样 -

在此输入图像描述

Here is the ggplot2 code I used to create the dot plot. 这是我用来创建点图的ggplot2代码。 There are two things I'd like to change: 我想改变两件事:

  1. I can plot the percentile values on the y -axis but I want these values on the x -axis (as shown in the graph above). 我可以在y轴上绘制百分位数值,但我想在x轴上显示这些值(如上图所示)。 Note that the coordinates are flipped. 请注意,坐标是翻转的。
  2. The axes don't display label for the minimum value (for example the percentile axis labels start at 25 when they should start at 0 instead.) 轴不显示最小值的标签(例如,百分位轴标签从25开始时应从0开始。)
# loading needed libraries
library(tidyverse)
library(ggstatsplot)

# creating dataframe with mean mileage per manufacturer
cty_mpg <- ggplot2::mpg %>%
  dplyr::group_by(.data = ., manufacturer) %>%
  dplyr::summarise(.data = ., mileage = mean(cty, na.rm = TRUE)) %>%
  dplyr::rename(.data = ., make = manufacturer) %>%
  dplyr::arrange(.data = ., mileage) %>%
  dplyr::mutate(.data = ., make = factor(x = make, levels = .$make)) %>%
  dplyr::mutate(
    .data = .,
    percent_rank = (trunc(rank(mileage)) / length(mileage)) * 100
  ) %>%
  tibble::as_data_frame(x = .)

# plot
ggplot2::ggplot(data = cty_mpg, mapping = ggplot2::aes(x = make, y = mileage)) +
  ggplot2::geom_point(col = "tomato2", size = 3) + # Draw points
  ggplot2::geom_segment(
    mapping = ggplot2::aes(
      x = make,
      xend = make,
      y = min(mileage),
      yend = max(mileage)
    ),
    linetype = "dashed",
    size = 0.1
  ) + # Draw dashed lines
  ggplot2::scale_y_continuous(sec.axis = ggplot2::sec_axis(trans = ~(trunc(rank(.)) / length(.)) * 100, name = "percentile")) +
  ggplot2::coord_flip() +
  ggplot2::labs(
    title = "City mileage by car manufacturer",
    subtitle = "Dot plot",
    caption = "source: mpg dataset in ggplot2"
  ) +
  ggstatsplot::theme_ggstatsplot()

Created on 2018-08-17 by the reprex package (v0.2.0.9000). reprex包创建于2018-08-17(v0.2.0.9000)。

I am not 100% sure to have understood what you really want, but below is my attempt to reproduce the first picture with mpg data: 我不是百分百肯定已经理解了你真正想要的东西,但下面是我尝试用mpg数据重现第一张照片:

require(ggplot2)

data <- aggregate(cty~manufacturer, mpg, FUN = mean)
data <- data.frame(data[order(data$cty), ], rank=1:nrow(data))

g <- ggplot(data, aes(y = rank, x = cty))
g <- g + geom_point(size = 2)
g <- g + scale_y_continuous(name = "Manufacturer", labels = data$manufacturer, breaks = data$rank,
                            sec.axis = dup_axis(name = element_blank(),
                                                breaks = seq(1, nrow(data), (nrow(data)-1)/4),
                                                labels = 25 * 0:4))
g <- g + scale_x_continuous(name = "Mileage", limits = c(10, 25),
                            sec.axis = dup_axis(name = element_blank()))
g <- g + theme_classic()
g <- g + theme(panel.grid.major.y = element_line(color = "black", linetype = "dotted"))

print(g)

That produces: 这产生:

在此输入图像描述

data <- aggregate(cty~manufacturer, mpg, FUN = mean)
data <- data.frame(data[order(data$cty), ], rank=1:nrow(data))

These two lines generate the data for the graph. 这两行生成图表的数据。 Basically we need the manufacturers, the mileage (average of cty by manufacturer ) and the rank. 基本上,我们需要的厂家,里程(平均ctymanufacturer )和等级。

g <- g + scale_y_continuous(name = "Manufacturer", labels = data$manufacturer, breaks = data$rank,
                            sec.axis = dup_axis(name = element_blank(),
                                                breaks = seq(1, nrow(data), (nrow(data)-1)/4),
                                                labels = 25 * 0:4))

Note that here the scale is using rank and not the column manufacturer . 请注意,此处的比例是使用rank不是manufacturer To display the name of the manufacturers, you must use the labels property and you must force the breaks to be for every values (see property breaks ). 要显示制造商的名称,必须使用labels属性,并且必须强制中断每个值(请参阅属性breaks )。

The second y-axis is generated using the sec.axis property. 使用sec.axis属性生成第二个y-axis This is very straight-forward using dup_axis that easily duplicate the axis. 使用可轻松复制轴的dup_axis非常简单。 By replacing the labels and the breaks , you can display the %-value. 通过替换labelsbreaks ,您可以显示%-value。

g <- g + theme(panel.grid.major.y = element_line(color = "black", linetype = "dotted"))

The horizontal lines are just the major grid. 水平线只是主要网格。 This is much easier to manipulate than geom_segments in my opinion. 在我看来,这比geom_segments更容易操作。

Regarding your question 1, you can flip the coordinates easily using coord_flip , with minor adjustments. 关于您的问题1,您可以使用coord_flip轻松翻转坐标,并进行微调。 Replace the following line: 替换以下行:

g <- g + theme(panel.grid.major.y = element_line(color = "black", linetype = "dotted")

By the following two lines: 通过以下两行:

g <- g + coord_flip()
g <- g + theme(panel.grid.major.x = element_line(color = "black", linetype = "dotted"),
               axis.text.x = element_text(angle = 90, hjust = 1))

Which produces: 哪个产生:

在此输入图像描述

Regarding your question 2, the problem is that the value 0% is outside the limits. 关于你的问题2,问题是0%的值超出了限制。 You can solve this issue by changing the way you calculate the percentage (starting from zero and not from one), or you can extend the limit of your plot to include the value zero, but then no point will be associated to 0%. 您可以通过更改计算百分比的方式(从零开始而不是从一个开始)来解决此问题,或者您可以扩展绘图的限制以包括零值,但是没有点将与0%相关联。

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

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