我想用百分位数创建一个点图,看起来像这样-
这是我用来创建点图的ggplot2
代码。我想更改两件事:
y
轴上绘制百分比值,但是我想要这些
x
轴上的值(如上图所示)。 请注意
坐标被翻转。 # 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()
由reprex package(v0.2.0.9000)创建于2018-08-17。
答案 0 :(得分:3)
我不是100%地确定您真正想要什么,但是下面是我尝试使用 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)
产生:
data <- aggregate(cty~manufacturer, mpg, FUN = mean)
data <- data.frame(data[order(data$cty), ], rank=1:nrow(data))
这两行生成图形的数据。基本上,我们需要制造商,里程(cty
manufacturer
的平均值)和排名。
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))
请注意,此处的标度是使用rank
而不是manufacturer
列。要显示制造商的名称,必须使用labels
属性,并且必须强制每个值都使用分隔符(请参阅属性breaks
)。
第二个y-axis
是使用sec.axis
属性生成的。使用dup_axis
非常简单,可以很容易地复制轴。通过替换labels
和breaks
,可以显示%值。
g <- g + theme(panel.grid.major.y = element_line(color = "black", linetype = "dotted"))
水平线只是主要的网格。在我看来,这比geom_segments容易得多。
关于问题1,您可以使用coord_flip
进行较小的调整即可轻松翻转坐标。替换以下行:
g <- g + theme(panel.grid.major.y = element_line(color = "black", linetype = "dotted")
通过以下两行:
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))
哪个会产生:
关于问题2,问题在于值0%
超出了限制。您可以通过更改百分比的计算方式来解决此问题(从零开始,而不是从一开始),或者可以扩展图的限制以包括零值,但是没有一点与0%关联。 / p>