📜  如何在R中的气泡图中命名所有圆圈?

📅  最后修改于: 2022-05-13 01:54:22.971000             🧑  作者: Mango

如何在R中的气泡图中命名所有圆圈?

在本文中,我们将了解如何在 R 编程语言中为气泡图中的所有圆圈命名。

要在 R 语言中的气泡图中的每个气泡上添加标签,我们使用 ggplot2 包的 geom_text()函数。 geom_text()函数在 ggplot 图的顶部添加文本注释重叠。

在下面的示例中,我们将创建一个数据框,然后使用此数据框绘制一个气泡图,每个气泡上都没有标签。

示例:基本图

R
# create sample data frame
x_value <- c(12,23,43,61,78,54,34,76,58)
y_value <- c(12,54,34,76,54,23,43,61,78)
radius <- c(1,5,13,8,12,3,2,16,7)
label <- c("Label1", "Label2", "Label3", 
           "label4", "label5", "label6",
           "label7", "label8", "label9")
sample_data <- data.frame( x_value, y_value, radius, label)
  
# load library ggplot2
library( ggplot2 )
  
# draw a basic bubble plot
ggplot( data = sample_data, aes( x=x_value, y=y_value, 
                                size=radius, color=label ) )+
        geom_point(alpha=0.4)+


R
# create sample data frame
x_value <- c(12,23,43,61,78,54,34,76,58)
y_value <- c(12,54,34,76,54,23,43,61,78)
radius <- c(1,5,13,8,12,3,2,16,7)
label <- c("Label1", "Label2", "Label3", "label4", "label5",
           "label6", "label7", "label8", "label9")
sample_data <- data.frame( x_value, y_value, radius, label)
  
# load library ggplot2
library( ggplot2 )
  
# draw a basic bubble plot
ggplot( data = sample_data, aes( x=x_value, y=y_value, 
                                size=radius, color=label ) )+
        geom_point(alpha=0.4)+
        geom_text( aes(label=label))


R
# create sample data frame
x_value <- c(12,23,43,61,78,54,34,76,58)
y_value <- c(12,54,34,76,54,23,43,61,78)
radius <- c(1,5,13,8,12,3,2,16,7)
label <- c("Label1", "Label2", "Label3", 
           "label4", "label5", "label6", 
           "label7", "label8", "label9")
sample_data <- data.frame( x_value, y_value, radius, label)
  
# load library ggplot2
library( ggplot2 )
  
# draw a basic bubble plot
ggplot( data = sample_data, aes( x=x_value, y=y_value,
                                size=radius, color=label ) )+
        geom_point(alpha=0.4)+
        scale_size(range = c(1,13) )+
        geom_text( aes(label=label), nudge_y= -3, nudge_x= -2)+
        theme(legend.position = "none")


输出:

现在将文本添加到我们使用的绘图中,geom_text()。

示例:向绘图添加文本

R

# create sample data frame
x_value <- c(12,23,43,61,78,54,34,76,58)
y_value <- c(12,54,34,76,54,23,43,61,78)
radius <- c(1,5,13,8,12,3,2,16,7)
label <- c("Label1", "Label2", "Label3", "label4", "label5",
           "label6", "label7", "label8", "label9")
sample_data <- data.frame( x_value, y_value, radius, label)
  
# load library ggplot2
library( ggplot2 )
  
# draw a basic bubble plot
ggplot( data = sample_data, aes( x=x_value, y=y_value, 
                                size=radius, color=label ) )+
        geom_point(alpha=0.4)+
        geom_text( aes(label=label))

输出:

为了调整气泡图中气泡标签的位置,我们使用 nudge_x 和 nudge_y。

示例:调整标签

R

# create sample data frame
x_value <- c(12,23,43,61,78,54,34,76,58)
y_value <- c(12,54,34,76,54,23,43,61,78)
radius <- c(1,5,13,8,12,3,2,16,7)
label <- c("Label1", "Label2", "Label3", 
           "label4", "label5", "label6", 
           "label7", "label8", "label9")
sample_data <- data.frame( x_value, y_value, radius, label)
  
# load library ggplot2
library( ggplot2 )
  
# draw a basic bubble plot
ggplot( data = sample_data, aes( x=x_value, y=y_value,
                                size=radius, color=label ) )+
        geom_point(alpha=0.4)+
        scale_size(range = c(1,13) )+
        geom_text( aes(label=label), nudge_y= -3, nudge_x= -2)+
        theme(legend.position = "none")

输出: