- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试手动将图例添加到我的 ggplot,但是,我一无所获。做了一些搜索,但找不到相关的解决方案。到目前为止,这是我的代码
library(tidyverse)
library(zoo)
FakeData = data.frame(A = runif(184, 50,100), B = A + 20, C = A + 40, D = A -20, E = A-10, F = A-15, G = seq(1:184))
lab=c("May", "Jun","July", "Aug","Sep","Oct", "Nov")
ggplot(FakeData, aes(G, ymin=A, ymax=B))+geom_ribbon(fill= "grey75")+geom_line(aes(G, C),linetype = "dashed", col = "red", size = 1.3)+
geom_line(aes(G, D),linetype = "dashed", colour = "black", size = 1.3)+
geom_line(aes(G, E), colour = "darkblue", size = 1.3)+ geom_point(aes(G, F), col = "blue", size = 1.3)+
scale_x_continuous(breaks = c(0,31,61,92,123,153,184), labels = lab)+ xlab("Month")+ ylab("Daily Cumulative Precipitation (mm)")+ theme_bw()
我想为图添加以下图例
Leg = c("Upper and lower Quartile", "Maximum", "Minimum", "Median", "Precipitation")
对 ggplot 的编辑
我根据建议修改了 ggplot 代码,但是,数据颜色与其图例不符。例如,颜色编码为红色的 Max 数据应该位于图中所有其他数据的顶部,但事实并非如此——对于其他图例也是如此。
ggplot(FakeData)+geom_ribbon(aes(G, ymin=A, ymax=B, col = "cyan"), alpha = 0.3)+geom_line(aes(G, C, col = "red"),linetype = "dashed", size = 1.3)+
geom_line(aes(G, D, col = "black"),linetype = "dashed", size = 1.3)+
geom_line(aes(G, E, col = "darkblue"), size = 1.3)+ geom_point(aes(G, F, col = "blue"), size = 1.3)+
scale_x_continuous(breaks = c(0,31,61,92,123,153,184), labels = lab)+ xlab("Month")+ ylab("Daily Cumulative Precipitation (mm)")+ theme_bw()+
scale_color_manual(labels = c("Upper and lower Quartile", "Maximum", "Minimum", "Median", "Precipitation"), values = c("cyan", "red","black", "darkblue","blue") )
最佳答案
要阐明图例中的不同颜色,您可以修改 Leg
向量以将颜色分配给特定标签(重命名为 CType
)。然后,您可以直接在 geom_line
和 geom_point
语句中提供标签,这将与图例中显示的内容相同。如评论中所述,我将 color
移到了图例的美学内部。要显示图例,您可以使用 scale_color_manual
。
编辑:要获得不同的线型 - 制作一个类似的矢量 LType
来指示哪种线型(虚线、实线等)。没有使用 geom_point
进行沉淀,而是使用了点线型 - 否则,很难将 color
和 linetype
图例组合成一个图例(除非你为 geom_point
指定 linetype
会引发警告但随后忽略)。为色带边界添加了额外的线条,因此它会显示在图例中。
library(tidyverse)
library(zoo)
set.seed(1)
N=184
A = runif(N, 50, 100)
G = seq(1:N)
FakeData = data.frame(A, B = A + 20, C = A + 40, D = A - 20, E = A + 10, F = A - 15, G)
lab=c("May", "Jun", "July", "Aug", "Sep", "Oct", "Nov")
CType = c("Upper and lower Quartile" = "grey75", "Maximum" = "red", "Minimum" = "black", "Median" = "darkblue", "Precipitation" = "blue")
LType = c("Upper and lower Quartile" = "solid", "Maximum" = "dashed", "Minimum" = "dashed", "Median" = "solid", "Precipitation" = "dotted")
ggplot(FakeData, aes(x = G))+
geom_ribbon(aes(ymin = A, ymax = B), fill= "grey75")+
geom_line(aes(y = A, colour = "Upper and lower Quartile", linetype = "Upper and lower Quartile"), size = 1.3)+
geom_line(aes(y = B, colour = "Upper and lower Quartile", linetype = "Upper and lower Quartile"), size = 1.3)+
geom_line(aes(y = C, colour = "Maximum", linetype = "Maximum"), size = 1.3)+
geom_line(aes(y = D, colour = "Minimum", linetype = "Minimum"), size = 1.3)+
geom_line(aes(y = E, colour = "Median", linetype = "Median"), size = 1.3)+
geom_line(aes(y = F, colour = "Precipitation", linetype = "Precipitation"), size = 1.3)+
scale_x_continuous(breaks = c(0,31,61,92,123,153,184), labels = lab)+
xlab("Month")+
ylab("Daily Cumulative Precipitation (mm)")+
theme_bw()+
scale_color_manual(name = "legend", values = CType)+
scale_linetype_manual(name = "legend", values = LType)+
theme(legend.position = c(.2, .8), legend.title = element_blank(), legend.key.width = unit(2, "cm"))
关于r - 如何将用户定义的图例添加到 R 中的 ggplot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58001307/
我在 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 ,并且术语“男性”和“女性”出现在正确的表情符号
我是一名优秀的程序员,十分优秀!