ggplot2几何图R散点图中的标记点

ggplot2几何图R散点图中的标记点,r,ggplot2,scatter-plot,R,Ggplot2,Scatter Plot,如何使用数字而不是颜色标记散点图中的点 下面是我正在使用的代码,而不是说明什么颜色与我希望它使用数字的更改相关的图例。很难说它是什么颜色,因为我用的是彩色面板 代码: 我希望每个项目,即“添加服务器”对应一个唯一的整数,然后绘制该整数。谢谢 编辑: 数据帧结构: 列:更改(字符串)、影响(浮动)、可能性(浮动) 我想不出一种只使用ggplot2函数的方法,但也许有一种优雅的方法可以做到这一点。相反,您可以使用gridExtra和tableGrob来显示正确的图例 我将您对geom_point

如何使用数字而不是颜色标记散点图中的点

下面是我正在使用的代码,而不是说明什么颜色与我希望它使用数字的更改相关的图例。很难说它是什么颜色,因为我用的是彩色面板

代码:

我希望每个项目,即“添加服务器”对应一个唯一的整数,然后绘制该整数。谢谢

编辑:

数据帧结构:

列:更改(字符串)、影响(浮动)、可能性(浮动)


我想不出一种只使用ggplot2函数的方法,但也许有一种优雅的方法可以做到这一点。相反,您可以使用gridExtra和
tableGrob
来显示正确的图例

我将您对
geom_point()
的调用替换为对
geom_text()
的调用,转换为一个grob,然后用图例中显示的文本创建一个表格grob,最后排列两个grob

# load your data as d and df

library(grid)
library(gridExtra)

# add in a Label column with numbers
df$Label <- 1:nrow(df)

g2 <- ggplot() + 
  geom_text(data = df, aes(x = Impact, y = Likelihood, label = Label)) +
  scale_x_continuous(
    name = "Impact", 
    limits = c(.5,3.5),
    breaks=seq(.5,3.5, 1), 
    labels = seq(.5,3.5, 1)
  ) + 
  scale_y_continuous(
    name = "Likelihood", 
    limits = c(.5,3.2),
    breaks=seq(.5, 3.2, 1), 
    labels = seq(.5, 3.2, 1)
  ) +
  geom_rect(
    data = d, 
    mapping = aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2, fill = t), 
    alpha = .5, 
    color = "black"
  ) +
  geom_text(data = d, aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r), size=4)

g2_grob <- ggplotGrob(g2)

# pasted the two columns together for it to appear a little nicer
tab_leg <- tableGrob(
  paste(df$Label,"-", df$Change),
  theme = ttheme_minimal(
    core = list(fg_params = list(hjust=0, x=0.1,fontsize=8))
  )
)

# arrange the plot and table
grid.arrange(arrangeGrob(
  g2_grob, nullGrob(), tab_leg, nullGrob(),
  layout_matrix = matrix(1:4, ncol = 4), 
  widths = c(6,.5,2,1)
))
#将数据加载为d和df
图书馆(网格)
图书馆(gridExtra)
#添加带有数字的标签列

df$Label您可以在更改和颜色之间保持美学映射,以创建图例,同时将该层设置为不可见,以使其不影响整体画面:

df$ID <- seq(1, nrow(df))
df$Legend <- paste0(df$ID, ". ", df$Change)
df$Legend <- factor(df$Legend,
                    levels = df$Legend[order(df$ID)])

p <- ggplot() + 

  # text layer to position the numbers
  geom_text(data = df,
            aes(x = Impact, y = Likelihood, label = ID)) +

  # invisible layer to create legend for the numbers
  geom_point(data = df,
             aes(x = Impact, y = Likelihood, colour = Legend),
             alpha = 0, size = 0) +

  # rest of the code is unchanged
  scale_x_continuous(name = "Impact", limits = c(.5,3.5),
                     breaks=seq(.5,3.5, 1), labels = seq(.5,3.5, 1)) + 
  scale_y_continuous(name = "Likelihood", limits = c(.5,3.2),
                     breaks=seq(.5, 3.2, 1), labels = seq(.5, 3.2, 1)) +
  geom_rect(data=d, 
            aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2, fill = t), 
            alpha = .5, color = "black") +
  geom_text(data=d, 
            aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r), 
            size=4)

p

请你也发布
df
的结构好吗?嗨,杰克,我已经用数据和数据类型的图像更新了帖子。我希望这有帮助。我考虑添加一个额外的列,其中包含唯一整数的键,但我不确定集成R语法会是什么样子。谢谢。请您在R控制台中键入
dput(df)
,然后将输出复制并粘贴到问题中,而不是张贴数据的图片,好吗?这有助于我们更好地帮助您,使您的数据更易于使用。
# load your data as d and df

library(grid)
library(gridExtra)

# add in a Label column with numbers
df$Label <- 1:nrow(df)

g2 <- ggplot() + 
  geom_text(data = df, aes(x = Impact, y = Likelihood, label = Label)) +
  scale_x_continuous(
    name = "Impact", 
    limits = c(.5,3.5),
    breaks=seq(.5,3.5, 1), 
    labels = seq(.5,3.5, 1)
  ) + 
  scale_y_continuous(
    name = "Likelihood", 
    limits = c(.5,3.2),
    breaks=seq(.5, 3.2, 1), 
    labels = seq(.5, 3.2, 1)
  ) +
  geom_rect(
    data = d, 
    mapping = aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2, fill = t), 
    alpha = .5, 
    color = "black"
  ) +
  geom_text(data = d, aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r), size=4)

g2_grob <- ggplotGrob(g2)

# pasted the two columns together for it to appear a little nicer
tab_leg <- tableGrob(
  paste(df$Label,"-", df$Change),
  theme = ttheme_minimal(
    core = list(fg_params = list(hjust=0, x=0.1,fontsize=8))
  )
)

# arrange the plot and table
grid.arrange(arrangeGrob(
  g2_grob, nullGrob(), tab_leg, nullGrob(),
  layout_matrix = matrix(1:4, ncol = 4), 
  widths = c(6,.5,2,1)
))
df$ID <- seq(1, nrow(df))
df$Legend <- paste0(df$ID, ". ", df$Change)
df$Legend <- factor(df$Legend,
                    levels = df$Legend[order(df$ID)])

p <- ggplot() + 

  # text layer to position the numbers
  geom_text(data = df,
            aes(x = Impact, y = Likelihood, label = ID)) +

  # invisible layer to create legend for the numbers
  geom_point(data = df,
             aes(x = Impact, y = Likelihood, colour = Legend),
             alpha = 0, size = 0) +

  # rest of the code is unchanged
  scale_x_continuous(name = "Impact", limits = c(.5,3.5),
                     breaks=seq(.5,3.5, 1), labels = seq(.5,3.5, 1)) + 
  scale_y_continuous(name = "Likelihood", limits = c(.5,3.2),
                     breaks=seq(.5, 3.2, 1), labels = seq(.5, 3.2, 1)) +
  geom_rect(data=d, 
            aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2, fill = t), 
            alpha = .5, color = "black") +
  geom_text(data=d, 
            aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r), 
            size=4)

p
p + scale_color_discrete(guide = guide_legend(keywidth = unit(0, "pt")))