- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试手动更改 ggplot2 线图中图例的第一项的颜色。
我对一个变量有几个观察结果,我在一个线图中显示,就像这样:
ggplot(tmp1, aes(x=factor(month), y=value, group=variable, colour=variable ) ) +
geom_line(size=1) + geom_point(size=2.5) + theme_grey(base_size = 18) +
xlab(NULL) + ylab('%') + theme(legend.title = element_blank()) + theme(axis.text.x=element_blank()) +
ggtitle("a) Cloud fraction") + theme(plot.title = element_text(hjust = 0))
但是,第一个变量 (CRU
) 是我的引用,我想用黑色显示它的行。我设法通过添加一个带有条件 variable=='CRU'
的额外 geom_line 来做到这一点:
ggplot(tmp1, aes(x=factor(month), y=value, group=variable, colour=variable ) ) +
geom_line(size=1) + geom_point(size=2.5) + theme_grey(base_size = 18) +
geom_line(data=subset(tmp1, variable == "CRU"), colour="black", linetype="solid", size=1) +
geom_point(data=subset(tmp1, variable == "CRU"), colour="black", size=2.5) +
xlab(NULL) + ylab('%') + theme(legend.title = element_blank()) + theme(axis.text.x=element_blank()) +
ggtitle("a) Cloud fraction") + theme(plot.title = element_text(hjust = 0))
适用于线条,但图例保留旧颜色。
如何仅更改图例第一个元素的颜色,以匹配新的黑线?
这是我的数据示例:
library(ggplot2)
tmp1 <- structure(list(month = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L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, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L
), .Label = c("CRU", "CanESM2", "GFDL-ESM2M", "GISS-E2-H", "GISS-E2-R-CC",
"GISS-E2-R", "HadGEM2-AO", "HadGEM2-CC", "IPSL-CM5A-MR", "IPSL-CM5B-LR",
"MIROC4h", "MRI-CGCM3", "NorESM1-M", "bcc-csm1-1-m", "bcc-csm1-1",
"inmcm4"), class = "factor"), value = c(68.9226631460789, 68.2418796877392,
68.3045372212868, 66.5727907036073, 64.278360290491, 60.6452267972856,
56.4079999829923, 57.4384828307567, 60.874295882443, 63.70427487797,
65.9934520468731, 68.9723871966257, 69.0959015590216, 68.6126351492122,
65.9106136896166, 65.790169283913, 64.6320994816801, 63.894111784301,
62.0459530253135, 60.0455773681386, 59.4195693791228, 59.8531302282566,
62.8877658601921, 66.4625078340445, 63.4659654507164, 64.5466810117518,
63.6412932878715, 61.5786848043378, 60.6491980933614, 63.5160886052168,
62.739218138279, 60.8826348052995, 60.1196738813257, 59.0451443027396,
58.9044684656519, 61.5033887899156, 62.442928703121, 61.9933297554931,
61.686560285787, 62.1675956585161, 63.0625380934021, 63.3192922622326,
62.6727899590586, 60.9706714311941, 59.4656895840826, 59.8689092461429,
60.7585523645951, 62.2374164636759, 62.2586495696979, 62.3005886556949,
62.0719314334763, 61.7786313583016, 62.1037020616999, 62.5919637033876,
60.7746642298107, 58.7307471416832, 57.6602849809809, 57.3379551651851,
59.8210398283061, 61.5997238276034, 62.1190176575675, 62.2214930174241,
61.607539296931, 61.836536870373, 61.8298589429815, 62.0478835210295,
60.8165122782774, 59.224498365607, 57.5387307267022, 56.8641846144649,
59.6779581588162, 61.5822371331742, 56.9625864272884, 55.0519081266715,
53.9161532646461, 52.0847886852487, 54.1855963059705, 54.1565901942167,
53.8164314129289, 53.3013959169719, 52.1283494730607, 49.9814907883562,
51.0053330490513, 54.1758812796363, 54.1947459143536, 53.2985061657513,
51.5351727215781, 51.2131541342776, 53.040182168441, 53.4657505459587,
52.8257974728027, 52.8523832284788, 51.2527233914323, 48.0999294191007,
48.3915726340961, 50.9305288780026, 65.3647375158419, 64.6894843930494,
62.2700707798592, 60.2848148985731, 59.0797813854392, 58.6641353922813,
60.36671822738, 61.0883458866571, 60.3963355506111, 60.989444946264,
62.1570976843054, 64.0549504714623, 63.043822206253, 61.5388900651697,
61.0125502971802, 60.4999006674972, 60.9554692113674, 61.2665703834057,
61.1470225339614, 61.4827838311531, 60.0397138517742, 61.6503963603034,
62.7421837830534, 63.9911949044232, 55.7117557057576, 55.0687784028633,
51.7447044604762, 50.5160095376821, 51.7744811245234, 52.6710116909617,
52.9126480516047, 51.6347065362984, 50.6773480024225, 48.8928054774924,
50.3505731163001, 53.7488684714513, 61.558109087334, 61.6673093977654,
61.008465555097, 58.5478578294864, 57.4119260976748, 57.9275733769477,
56.9129774651439, 55.6494927089111, 52.0222406797903, 51.9215916366208,
53.4679949695072, 58.2128251869788, 64.7955701998493, 62.8319013929061,
60.8391061131818, 56.1759467734789, 55.4331550199683, 55.8437923896573,
54.998540828777, 54.7840203124691, 54.3853750266133, 52.7590435522892,
56.1409799671355, 62.0047140533332, 57.5185465474672, 57.2532289998115,
55.9911913829976, 54.6479285609432, 53.1659722964534, 53.3609799276622,
51.321452599498, 49.6933914680193, 48.6718229103421, 49.5393207890844,
52.8096091918065, 56.1667672797739, 60.7380412023987, 60.1791897430251,
58.7798069796932, 58.061108119255, 59.7770862278418, 60.2070273632675,
59.074898814382, 55.5571990297011, 53.8564792650491, 54.0753885029223,
56.2369958393563, 58.9062125901571, 70.7538119957697, 69.4271857400385,
67.3954189057409, 66.9262104442679, 67.1558044757422, 65.8848885390536,
65.3092556552615, 64.3799468889004, 64.9999333535186, 65.6493831700943,
69.2646980549075, 70.6342115226731)), row.names = c(NA, -192L
), .Names = c("month", "variable", "value"), class = "data.frame")
最佳答案
无需拆分数据并绘制两个 geom_line
,您可以简单地提供一个自定义调色板,其中 CRU
映射到黑色。
如果你想为其他变量保留默认颜色,你首先需要定义一个小辅助函数来检索它们的方式 ggplot2 does it .
gg_color_hue <- function(n) {
hues = seq(15, 375, length=n+1)
hcl(h=hues, l=65, c=100)[1:n]
}
然后创建自定义调色板矢量,结合标准调色板和黑色。由于 CRU
是因子变量的第一级(总共 16 级),这很简单
custom_palette <- c("#000000", gg_color_hue(15))
然后生成您想要的图:
ggplot(tmp1, aes(x=factor(month), y=value, group=variable, colour=variable)) +
geom_line(size=1) +
geom_point(size=2.5) +
scale_colour_manual(values=custom_palette) +
theme_grey(base_size = 18) +
xlab(NULL) + ylab('%') +
theme(legend.title = element_blank()) +
theme(axis.text.x=element_blank()) +
ggtitle("a) Cloud fraction") +
theme(plot.title = element_text(hjust = 0))
关于R - geom_line ggplot2 中的手动图例颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30371354/
我在 visual studio 中使用它,但编译器声称 h1 不能嵌套在 legend 元素中,但浏览器无论如何都可以呈现它,所以我很困惑这是有效的还是不是? Caption
Matplotlib 中的图例是帮助观察者理解图像数据的重要工具。 图例通常包含在图像中,用于解释不同的颜色、形状、标签和其他元素. 1. 主要参数 当不设置图例的参数时,默认的图例是这样的.
mongoDB分页的两种方法 mongoDB的分页查询是通过limit(),skip(),sort()这三个函数组合进行分页查询的 下面这个是我的测试数据 第一种方法 查询第一页的数据:db.te
我的 grafana 仪表板图例中有这些名称: stats.gauges.all.cloudwatch.communicationapi.dynamodb.ca_communicationapi_re
鉴于以下情况: import pandas as pd import matplotlib.pyplot as plt d=pd.DataFrame({'category':['a','a','a',
我收到此错误,请问出了什么问题? plt.legend(handles=[d1, d2]) File "/usr/lib/python3/dist-packages/matplotlib/pypl
这个问题已经有答案了: python matplotlib: label in histogram (1 个回答) 已关闭 5 年前。 我无法向使用 matplotlib.pyplot 创建的直方图添
我想更改我的图例,我必须删除分页并将所有标题放在一两行中。我想更改图标。 现在是这个样子 我想要这样的东西 我输入了 scrollArrows: 'none' 但它不起作用。这是我在 Controll
我正在创建一个文件来读取一定数量的 .wav 文件,每个文件对应一个音符。我正在对它们中的每一个执行 FFT,并将它们全部绘制在同一个图形上。但是,我在正确打印图例时遇到了问题,它将我想要使用的名称分
我正在 ggplot 中处理多个 sf 几何图形,并希望以点、线和正方形(对于多边形)的形式显示图例。然而,geom_sf图例结合了我的几何特征(即结合线和点)如下所示: library(ggplot
我想要 geom_text()标签根据变量采用字体系列。 根据 ggplot2 docs 上的示例(向下滚动到底部),我已经这样做了(与 ggplot 文档示例中的相同): library(ggplo
我正在绘制一个 geom_tile 图,其中每个点都有一个特定的 p-value。 图的绘制方式很好,p-value 限制在图例上。 我想添加到这个图例的是具有这个 p-value 的数据点的数量。
我有以下 data.frame : my.df = data.frame(mean = c(0.045729661,0.030416531,0.043202944,0.025600973,0.0405
我正在绘图中打印 Case-Shiller 索引: structure(list(Date = structure(c(10957, 11048, 11139, 11231, 11323, 1141
我想绘制一个包含 4 个不同值( 1 )的栅格,并带有描述类别的分类文本图例,例如 2但有彩盒: 我试过使用图例,例如: legend( 1,-20,legend = c("land","ocean/
我在 R 中用 ggplot2 制作了这个图 由以下代码绘制: ggplot(mtcars) + geom_smooth(fill='grey', alpha=0.3, span=0.1, aes
我有来自两个不同数据框的数据。我正在尝试为每个数据框创建图例。我知道我可以组合数据框并进行操作,但是由于我的数据源,从两个不同的数据框进行绘图最有意义。 请在下面找到简化的示例。我已经接近了,但传说中
我将 highchart 的图形保存为 svg。 var chart = $('#graphicName').highcharts(); EXPORT_WIDTH = 1000; render_wid
我想将自定义符号设置为图例项。目前我有矩形。我需要带有勾号的矩形(如彩色复选框)。根据文档,我可以自定义元素,例如符号附近的文本。 但如果我将绘图上的所有元素都设置为自定义图像 ( Example )
我有下面的代码,可以生成饼图和图例。有没有办法让表情符号和饼图具有相同的对应颜色,因为目前表情符号是黑色的。另外,我希望删除图例中的方形色 block ,并且术语“男性”和“女性”出现在正确的表情符号
我是一名优秀的程序员,十分优秀!