散点图中的ggplot2 Geom_Plot R标签点

时间:2018-11-29 16:33:45

标签: r ggplot2 scatter-plot

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

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

代码:

d=data.frame(x1=c(.5,2,.5,2), 
             x2 = c(2,3.5,2,3.5), 
             y1 = c(.5,.5,2,2), 
             y2 = c(2,2,3.2,3.2), 
             t=c('low,low','high,low','low,high','high,high'), 
             r=c('low,low','high,low','low,high','high,high'))

ggplot() + 
  geom_point(data = df, aes(x=df$Impact, y=df$Likelihood, colour = df$Change)) + 
  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)

Current Plot

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

编辑:

数据框结构:

Image of the Data

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

dput(df)
structure(list(Change = c("Windows Patches\n-CRPDB1", "Change DNS settings", 
"SSIS Schedule change\n-Warehouse", "OnBase Upgrade", "Add Server", 
"Change IL Parameter", "Code Change - Validation missing", "Mass Update Data in Infolease", 
"User add, remove or update user permission", "ServiceNow Deployment", 
"Creating of a sever or desktop image for mass deployment", "Database table update. Column add/modify", 
"Update add PRTG/Sensor"), Impact = c(3, 1.8, 2.6, 2.3, 1, 2.25, 
1.8, 1.95, 1.3, 1.5, 1.8, 1, 1), Likelihood = c(3, 1.75, 1.7, 
1.6, 1.3, 1.15, 1.15, 1.15, 1.15, 1.1, 1, 1, 1)), class = "data.frame", row.names = c(NA, 
-13L))

1 个答案:

答案 0 :(得分:1)

我无法想到仅使用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)
))

如果要移动区域图例,可以查看以下答案:Show the table of values under the bar plot