Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2统计和:用百分比标记点_R_Ggplot2_Labels - Fatal编程技术网

R ggplot2统计和:用百分比标记点

R ggplot2统计和:用百分比标记点,r,ggplot2,labels,R,Ggplot2,Labels,我想问一下,是否有可能在stat_sum绘制的每个点上标注该点所代表的观察值的百分比(即比例)。理想情况下,我希望标签采用百分比格式,而不是十进制格式 非常感谢您抽出时间 编辑:最小可复制示例 library("ggplot2") library("scales") ggplot(diamonds, aes(x = cut, y = clarity)) + stat_sum(aes(group = 1)) + scale_size_continuous(labels=percent)

我想问一下,是否有可能在stat_sum绘制的每个点上标注该点所代表的观察值的百分比(即比例)。理想情况下,我希望标签采用百分比格式,而不是十进制格式

非常感谢您抽出时间

编辑:最小可复制示例

library("ggplot2")
library("scales")

ggplot(diamonds, aes(x = cut, y = clarity)) +
  stat_sum(aes(group = 1)) +
  scale_size_continuous(labels=percent)


因此,我的问题是,如何(如果可能的话)用它们的“道具”百分比值来标记这些汇总点。

有几个选项。我将假定,由于这些点标有百分比计数,因此不需要图例

一个选项是添加另一个
stat\u sum()
函数,该函数包含一个标签和一个“文本”几何图形。例如:

library("ggplot2")

ggplot(diamonds, aes(x = cut, y = clarity, group = 1)) +
  stat_sum(geom = "point", show.legend = FALSE) +
  stat_sum(aes(label = paste(round(..prop.. * 100, 2), "%", sep = "")), 
              size = 3, hjust = -0.4, geom = "text", show.legend = FALSE)
或者,可能不需要积分。标签可以完成所有工作-显示位置和大小:

ggplot(diamonds, aes(x = cut, y = clarity, group = 1)) +
   stat_sum(aes(label = paste(round(..prop.. * 100, 2), "%", sep = "")), 
              geom = "text", show.legend = FALSE) +
  scale_size(range=c(2, 8))
有时在ggplot之外创建汇总表更容易:

library(plyr)
df = transform(ddply(diamonds, .(cut, clarity), "nrow"),
        percent = round(nrow/sum(nrow)*100, 2))

ggplot(df, aes(x = cut, y = clarity)) +
  geom_text(aes(size = percent, label = paste(percent, "%", sep = "")), 
                     show.legend = FALSE) +
  scale_size(range = c(2, 8))

也许您可以给出一些(可复制的)示例代码?抱歉,我无法共享我使用的数据,但我生成的图与文档中的图完全相同:。第二个示例图代表了我所拥有的。我希望能够用它们的prop值来标记这些总结点中的每一个,尽管最好是百分比格式。我希望这有助于详细说明。@Harry你的问题已经有16个小时没有得到回答,尽管这应该很容易解决,可能是因为你没有提供一个最小的可重复的例子。我建议您创建一些虚假数据(一小部分)和最低限度的支持代码,然后将它们添加到您的问题中。作为另一个新手,这会提高你得到答案的机会。也请看这个@SlowLearner谢谢你的建议,我添加了一个使用内置于ggplot2的钻石数据集的示例。