首页 文章

在对连续变量进行离散化后,反转ggplot色标中的标签顺序[重复]

提问于
浏览
1

这个问题在这里已有答案:

请考虑以下示例数据集:

mydata="theta,rho,Response
0,0.8400000,0.0000000
40,0.8400000,0.4938922
80,0.8400000,0.7581434
120,0.8400000,0.6675656
160,0.8400000,0.2616592
200,0.8400000,-0.2616592
240,0.8400000,-0.6675656
280,0.8400000,-0.7581434
320,0.8400000,-0.4938922
0,0.8577778,0.0000000
40,0.8577778,0.5152213
80,0.8577778,0.7908852
120,0.8577778,0.6963957
160,0.8577778,0.2729566
200,0.8577778,-0.2729566
240,0.8577778,-0.6963957
280,0.8577778,-0.7908852
320,0.8577778,-0.5152213
0,0.8755556,0.0000000
40,0.8755556,0.5367990
80,0.8755556,0.8240077
120,0.8755556,0.7255612
160,0.8755556,0.2843886
200,0.8755556,-0.2843886
240,0.8755556,-0.7255612
280,0.8755556,-0.8240077
320,0.8755556,-0.5367990
0,0.8933333,0.0000000
40,0.8933333,0.5588192
80,0.8933333,0.8578097
120,0.8933333,0.7553246
160,0.8933333,0.2960542
200,0.8933333,-0.2960542
240,0.8933333,-0.7553246
280,0.8933333,-0.8578097
320,0.8933333,-0.5588192
0,0.9111111,0.0000000
40,0.9111111,0.5812822
80,0.9111111,0.8922910
120,0.9111111,0.7856862
160,0.9111111,0.3079544
200,0.9111111,-0.3079544
240,0.9111111,-0.7856862
280,0.9111111,-0.8922910
320,0.9111111,-0.5812822
0,0.9288889,0.0000000
40,0.9288889,0.6041876
80,0.9288889,0.9274519
120,0.9288889,0.8166465
160,0.9288889,0.3200901
200,0.9288889,-0.3200901
240,0.9288889,-0.8166465
280,0.9288889,-0.9274519
320,0.9288889,-0.6041876
0,0.9466667,0.0000000
40,0.9466667,0.6275358
80,0.9466667,0.9632921
120,0.9466667,0.8482046
160,0.9466667,0.3324593
200,0.9466667,-0.3324593
240,0.9466667,-0.8482046
280,0.9466667,-0.9632921
320,0.9466667,-0.6275358
0,0.9644444,0.0000000
40,0.9644444,0.6512897
80,0.9644444,0.9997554
120,0.9644444,0.8803115
160,0.9644444,0.3450427
200,0.9644444,-0.3450427
240,0.9644444,-0.8803115
280,0.9644444,-0.9997554
320,0.9644444,-0.6512897
0,0.9822222,0.0000000
40,0.9822222,0.6751215
80,0.9822222,1.0363380
120,0.9822222,0.9125230
160,0.9822222,0.3576658
200,0.9822222,-0.3576658
240,0.9822222,-0.9125230
280,0.9822222,-1.0363380
320,0.9822222,-0.6751215
0,1.0000000,0.0000000
40,1.0000000,0.6989533
80,1.0000000,1.0729200
120,1.0000000,0.9447346
160,1.0000000,0.3702890
200,1.0000000,-0.3702890
240,1.0000000,-0.9447346
280,1.0000000,-1.0729200
320,1.0000000,-0.6989533"

foobar <- read.csv(text = mydata)

当然 Response 是一个连续变量,它应该用连续的色标绘制 . 但是,我被要求使用离散色标,因此我需要将 value 离散化 . 我的自然方法与此问题的第二个答案相同:

easiest way to discretize continuous scales for ggplot2 color scales?

library(ggplot2)
ggplot(data = foobar, aes(x = theta, y = rho, fill = cut(Response, breaks = 5))) +
geom_tile() + 
coord_polar(theta = "x", start = -pi/9) +
scale_x_continuous(breaks = seq(0, 360, by = 45)) +
scale_y_continuous(limits = c(0, 1)) +
scale_fill_brewer(palette = "RdYlGn", direction = -1, name = "Response")

enter image description here

但是,我希望标签按递减顺序绘制,即如果它是连续变量,则使用相同的顺序 ggplot2 . 在我的示例中,这意味着对应于红色的标签 (0.644, 1.08] 应位于顶部,对应于蓝色的标签 (-1.08, 0.644] 应位于图例的底部 . 我怎么能得到它?

1 回答

  • 1

    您可以使用 guide_legend 参数 reverse 来反转图例 .

    scale_fill_brewer(palette = "RdYlGn", direction = -1, name = "Response",
                            guide = guide_legend(reverse = TRUE))
    

相关问题