【问题标题】:how to plot matrices with ggplot2 and maintain the same colors for the same values如何使用 ggplot2 绘制矩阵并为相同的值保持相同的颜色
【发布时间】:2014-02-06 11:22:09
【问题描述】:

我正在使用 ggplot2 绘制存储在 12 个矩阵中的值。每个矩阵单元格可以包含一个从 1 到 10 的数字,我使用 ggplot2 来绘制这些值:

col<-c("red", "orange", "blue", "cyan", "green", "lightgreen", "violet", "pink", "black", "brown")
tdm <- melt(node10den0.7)
ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value))) +
labs(x = "Cost", y = "Benefit", fill = "Value", title="10 nodes, den=0.7") +
geom_raster() +
scale_fill_manual("Legend", breaks = c("1","2","3","4","5","6","7","8","9","10"),
                  values = col, labels=c("null", "n-null", "star", "n-star", "bistar", "n-bistar", "shared", "n-shared", "complete", "n-complete")) +
theme(plot.background = element_rect(fill = "grey90"),
      legend.background = element_rect(fill = "grey90")) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))

我的问题是并非所有矩阵都包含所有可能的值,所以我看到某些图中某些值的颜色发生了变化......有没有办法保持颜色固定?如果你在矩阵中看到 2 然后将其绘制为红色?

第二个问题:是否可以让ggplot2在图例中显示所有可能的颜色/标签?

我添加了几个矩阵作为示例:

> dput(node3den0)
structure(c(2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 
8, 2, 0, 2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 
0, 0, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 0, 0, 
0, 2, 2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 2, 2, 0, 0, 0, 0, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 10, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 
2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 2, 2, 2, 2, 2, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 2), .Dim = c(19L, 19L), .Dimnames = list(
c("0.05", "0.1", "0.15", "0.2", "0.25", "0.3", "0.35", "0.4", 
"0.45", "0.5", "0.55", "0.6", "0.65", "0.7", "0.75", "0.8", 
"0.85", "0.9", "0.95"), c("0.05", "0.1", "0.15", "0.2", "0.25", 
"0.3", "0.35", "0.4", "0.45", "0.5", "0.55", "0.6", "0.65", 
"0.7", "0.75", "0.8", "0.85", "0.9", "0.95")))

> dput(node10den0.7)
structure(c(3, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,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, 3, 2, 3, 2, 2, 10, 2, 
7), .Dim = c(19L, 19L), .Dimnames = list(c("0.05", "0.1", "0.15", 
"0.2", "0.25", "0.3", "0.35", "0.4", "0.45", "0.5", "0.55", "0.6", 
"0.65", "0.7", "0.75", "0.8", "0.85", "0.9", "0.95"), c("0.05", 
"0.1", "0.15", "0.2", "0.25", "0.3", "0.35", "0.4", "0.45", "0.5", 
"0.55", "0.6", "0.65", "0.7", "0.75", "0.8", "0.85", "0.9", "0.95"
)))

最好的问候, 西蒙娜

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    使用limits 参数。使用您的第一个矩阵:

    col<-c("red", "orange", "blue", "cyan", 
           "green", "lightgreen", "violet", "pink", "black", "brown", "yellow")
    ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value))) +
      labs(x = "Cost", y = "Benefit", fill = "Value", title="10 nodes, den=0.7") +
      geom_raster() +
      scale_fill_manual("Legend", limits = 0:10,
                        values = col, 
                        labels=c("null", "n-null", "star", "n-star", "bistar", 
                          "n-bistar", "shared", "n-shared", "complete", "n-complete",
                          "additional")) +
      theme(plot.background = element_rect(fill = "grey90"),
            legend.background = element_rect(fill = "grey90")) +
      scale_x_continuous(expand = c(0, 0)) +
      scale_y_continuous(expand = c(0, 0))
    

    请注意,我添加了一个颜色值,因为您的可能值似乎从 0 到 10。

    【讨论】:

    • 我有一个评论:你的情节看起来很方形,而在我的电脑上 x 和 y 轴看起来不像有相同的长度......
    • @SimoneGabbriellini 这取决于图形窗口的大小。使用ggsave 保存具有定义尺寸的绘图。
    • 谢谢,我正在尝试使用 ggsave,但在我的 mac osx 上,这会产生非常糟糕的 pdf... 在屏幕上,一切都很完美,但是当只保存轴和图例时,所有的其他的东西很模糊......
    • 我在家里的 mac 上从来没有遇到过这个问题,但你可以尝试使用不同的 pdf 设备。
    • 解决了这个问题,这是预览设置的问题...谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多