r-使用ggplot将点显示为值 [英] r - using ggplot to show a dot as the value

查看:85
本文介绍了r-使用ggplot将点显示为值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数据框:

I have a data frame like this:

CWSR = c(0.2, 0.1, 0.5, 0.6, 0.4, 0.8, 0.9, 0.7, 0.1, 0.2) 
BPA = c(1,5,9,8,4,3,2,1,4,3) 
df = data.frame(CWSR, BPA)

   CWSR BPA
1   0.2   1
2   0.1   1
3   0.5   9
4   0.1   2
5   0.4   4
6   0.1   2
7   0.9   2
8   0.1   3
9   0.1   2
10  0.2   3

我已经生成了下面的直方图,但是我想显示图形中每个值的点而不是整个条形,而不是生成的布局.目前我正在使用此:

I have generated the below histogram but instead of the layout produced, I want to display show a dot for each value in my graph, rather than an entire bar. Currently I am using this:

p <- ggplot(HData, aes(BPA, CWSR))
p + geom_bar(stat="identity")

此外,如果CWSR的值等于0.1,我希望计算BPA的实例数,然后将其显示为百分比.

In addition, I am looking to count the number of instances of BPA IF the value of CWSR is equal to 0.1, then to display this as a percentage.

因此,在上面的示例中,对于BPA值为1,对于CWSR 0.1的值(第2行),它会发生一次,因此它将显示为100%.对于2的BPA值,对于CWSR 0.1的值(第4,6,9行),这会发生3次,但BPA值为2,而CWSR的值不等于1(第7、0.9行),因此总百分比将会显示为75%(4分之3).

So in the example above, for BPA value of 1, this occurs once for the value of CWSR 0.1 (line 2) so this would show 100%. For the BPA value of 2, this occurs 3 times for the value of CWSR 0.1 (lines 4,6,9) BUT there is a BPA value of 2 and CWSR value not equal to 1 (line 7, 0.9) so the total percentage would show as 75% (3 out of 4).

我尝试过这样的事情:

df %>%
  group_by(BPA) %>% 
  summarise(num = n(),
            totalCWSR = sum(CWSR==1), totalP = (totalCWSR / num * 100)) 

但是不确定是否正确或如何在ggplot中显示吗?

But not sure if it is correct or how to display in ggplot?

我希望这很清楚,很抱歉,以前没有提供太多详细信息.

I hope this is clearer, apologies for not providing as much detail previously.

推荐答案

我不确定这不是您想要的,并且可能有更优雅的方法,但是这里有一个可能的解决方案给定BPA值,然后在CWSR中输入您的值,然后执行一个简单的 geom_point 绘图:

I'm not sure this is what you want, and there is probably a much more elegant way of doing this, but here you have one possible solution that aggregates your values in CWSR given the BPA values, and then does a simple geom_point plot:

library(dplyr)
library(ggplot2)

CWSR = c(0.2, 0.1, 0.5, 0.6, 0.4, 0.8, 0.9, 0.7, 0.1, 0.2) 
BPA = c(1,5,9,8,4,3,2,1,4,3) 
df = data.frame(CWSR, BPA)

df %>% group_by(BPA) %>%
  summarise(CWSRsum = sum(CWSR)) %>%
  arrange(BPA) %>%
  ggplot(aes(BPA, CWSRsum)) + geom_point(size=5)

这篇关于r-使用ggplot将点显示为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆