gpt4 book ai didi

r - 使用 geom_dotplot 更改点图的 y 轴以反射(reflect)实际计数

转载 作者:行者123 更新时间:2023-12-03 14:39:26 24 4
gpt4 key购买 nike

我正在尝试使用 geom_dotplot 创建点图的 ggplot2 .

但是,如 this page 上的示例所示,y 轴的刻度范围从 0 到 1。我想知道如何更改 y 轴刻度,以便这些值反射(reflect)数据的实际计数。

最佳答案

这是一个可能有用的示例。

library(ggplot2)
library(ggExtra)
library(dplyr)
# use the preloaded iris package in R
irisdot <- head(iris["Petal.Length"],15)
# find the max frequency (used `dplyr` package). Here n is the label for frequency returned by count().
yheight <- max(dplyr::count(irisdot, Petal.Length)["n"])
# basic dotplot (binwidth = the accuracy of the data)
dotchart = ggplot(irisdot, aes(x=Petal.Length), dpi = 600)
dotchart = dotchart + geom_dotplot(binwidth=0.1, method="histodot", dotsize = 1, fill="blue")
# use coor_fixed(ratio=binwidth*dotsize*max frequency) to setup the right y axis height.
dotchart = dotchart + theme_bw() + coord_fixed(ratio=0.1*yheight)
# tweak the theme a little bit
dotchart = dotchart + theme(panel.background=element_blank(),
panel.border = element_blank(),
panel.grid.minor = element_blank(),
# plot.margin=unit(c(-4,0,-4,0), "cm"),
axis.line = element_line(colour = "black"),
axis.line.y = element_blank(),
)
# add more tick mark on x axis
dotchart = dotchart + scale_x_continuous(breaks = seq(1,1.8,0.1))
# add tick mark on y axis to reflect frequencies. Note yheight is max frequency.
dotchart = dotchart + scale_y_continuous(limits=c(0, 1), expand = c(0, 0), breaks = seq(0, 1,1/yheight), labels=seq(0,yheight))
# remove x y lables and remove vertical grid lines
dotchart = dotchart + labs(x=NULL, y=NULL) + removeGridX()
dotchart

A dotplot for 15 iris petal lengths

我不知道它为什么有效。似乎 geom_dotplot 的 y 轴高度为 1。 x 和 y 之间的比率由 coor_fixed(ratio=binwidth * dotsize * max frequency) 设置。

关于r - 使用 geom_dotplot 更改点图的 y 轴以反射(reflect)实际计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49330742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com