简体   繁体   English

使用R和ggplot2在散点图中具有相似值的圆点

[英]Round points with similar values in a scatter plot using R and ggplot2

I have my data as XYZ triplet. 我的数据为XYZ三元组。 For each XY pair exists a Z value. 对于每个XY对,都有一个Z值。 I'd like to plot the XY pairs in a scatter plot and round te values like the following example 我想在散点图中绘制XY对,并舍入te值,如以下示例所示

在此处输入图片说明

In the MWE I'd like to round the Z values in 3 categories: 在MWE中,我想将Z值分为3类:

  • Less than 5 小于5
  • Less than 10 少于10
  • Less than 15 小于15

Any help please 任何帮助请

  library(ggplot2)

x <- c(1,1.2,1.1,2,2.1,2.1,2.9,3,3.2)
y <- rep(seq(0,8,4),3)
z <- c(2,3,5,8,7,9,13,15,12)

DF <- data.frame(x,y,z)

ggplot(DF, aes(x,y,z)) +
  geom_point() +
  geom_text(label = z,
            hjust = 0,
            nudge_x = 0.05,
            nudge_y = 0.05)

在此处输入图片说明

The library ggalt can help you doing the trick using the function geom_encircle() as follows: ggalt库可以使用geom_encircle()函数帮助您完成操作,如下所示:

library(ggplot2)
library("ggalt")

x <- c(1,1.2,1.1,2,2.1,2.1,2.9,3,3.2)
y <- rep(seq(0,8,4),3)
z <- c(2,3,5,8,7,9,13,15,12)

DF <- data.frame(x,y,z)

ggplot(DF, aes(x,y,z)) +
  geom_point() +
  geom_text(label = z,
            hjust = 0,
            nudge_x = 0.05,
            nudge_y = 0.05)+
geom_encircle(data=subset(DF, z<5), 
              color="red", 
              size=1, expand=0.04)+
geom_encircle(data=subset(DF, z<10), 
                color="blue", 
                size=1, 
                expand=0.06)+
geom_encircle(data=subset(DF, z<15), 
                color="black", 
                size=1, 
                expand=0.08)+ 
xlim(0,4)+ylim(-1,10)

在此处输入图片说明

For more information check https://rdrr.io/cran/ggalt/man/geom_encircle.html Cheers ! 有关更多信息,请查看https://rdrr.io/cran/ggalt/man/geom_encircle.html干杯!

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

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