- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在修改 ggplot2 图表中的图例时遇到了一些麻烦。具体来说,我想创建两个图例而不是一个图例并更改其中的名称。
我想绘制三条曲线以及从 x 轴到第一条曲线的两条垂直线。我使用的代码如下:
# generate data
X1 <- as.data.frame(matrix(nrow = 10, ncol = 2))
colnames(X1) <- c("Weight", "Height")
X1$Weight <- seq(1:10)
X1$Height <- c(2, 3, 3.5, 4, 3.8, 5, 6.1, 5.4, 7, 7.1)
X2 <- as.data.frame(matrix(nrow = 10, ncol = 2))
colnames(X2) <- c("Weight", "Height")
X2$Weight <- c(seq(1:8), NA, NA)
X2$Height <- c(4, 3.4, 3.1, 6, 5.4, 6, 6.3, 7.5, NA, NA)
X3 <- as.data.frame(matrix(nrow = 10, ncol = 2))
colnames(X3) <- c("Weight", "Height")
X3$Weight <- c(seq(1:10))
X3$Height <- c(7, 6.9, 6.8, 6.1, 7, 7.5, 7.8, 9, 9.2, 9.1)
# create plot
require(ggplot2)
mycurve <- qplot(Weight, Height, data = X1, geom = "line", color = "X1",
main = "Plot", xlab = "Weight [kg]", ylab = "Height [m]",
xlim = range(X1$Weight))
mycurve +
geom_line(aes(X2$Weight, X2$Height, color = "X2"),
linetype = "twodash", na.rm = TRUE) +
geom_line(aes(X3$Weight, X3$Height, color = "X3"),
linetype = "dotted", na.rm = TRUE) +
geom_segment(aes(x = 5, y = 0, color = "Y1", xend = 5, yend = X1["5", 2]),
linetype="longdash") +
geom_segment(aes(x = 7, y = 0, color = "Y2", xend = 7, yend = X1["7", 2]),
linetype="longdash") +
scale_color_manual(values=c("X1" = "black", "X2" = "darkseagreen4",
"X3" = "darkred", "Y1"="green2", "Y2"="blue")) +
theme_bw()
它给了我这个输出:
现在我想改变图例。我想要两个图例:
此外,我希望“对象”具有以下顺序:“对象 2”、“对象 1”、“对象 3”。
对于解决此问题的任何帮助,我将不胜感激!
最佳答案
我建议将您的数据合并到一个数据框中。传递给 ggplot()
更简洁:
# combine data
df <- rbind(X1, X2, X3)
df$Group <- rep(c("Object1", "Object2", "Object3"), each = 10)
df <- rbind(df,
data.frame(Weight = 5,
Height = c(0, X1["5", 2]),
Group = "Line1"),
data.frame(Weight = 7,
Height = c(0, X1["7", 2]),
Group = "Line2"))
在 ggplot 中,我们为每种刻度类型设计了一个图例,因此拥有两个线条颜色图例并不是自然而然的事情。本帖here讨论一些方法。我使用了 2nd solution :
# add legend groupings as unused factor levels
# also specify legend order
df$Group <- factor(df$Group, levels = c("Object",
"Object2", "Object1", "Object3",
" ",
"Lines",
"Line1", "Line2"))
此外,我建议使用 ggplot
而不是 qplot
。正如包的文档所述,qplot
被设计为一个方便的包装器,以与基本 plot
函数的语法保持一致,但 ggplot
更擅长处理更复杂的情节要求:
p <- ggplot(df,
aes(x = Weight, y = Height,
group = Group, linetype = Group, color = Group)) +
geom_line() +
scale_linetype_manual(values = c( # actual line types used in the plot
"Object1" = "solid",
"Object2" = "twodash",
"Object3" = "dotted",
"Line1" = "longdash",
"Line2" = "longdash",
# placeholder values for legend titles
"Object" = "solid", "Lines" = "solid", " " = "solid"),
drop = F) +
scale_color_manual(values = c( # actual line types used in the plot
"Object1" = "black",
"Object2" = "darkseagreen4",
"Object3" = "darkred",
"Line1" = "green2",
"Line2" = "blue",
# placeholder values for legend titles
"Object" = "white", "Lines" = "white", " " = "white"),
drop = F) +
labs(title = "Plot", x = "Weight [kg]", y = "Height [m]") +
theme_bw() +
theme(legend.title = element_blank())
p
编辑以包括更改单个图例标签:
我们可以对单个图例标签进行进一步更改,以使伪图例标题与其他“正常”标签更加不同。由于 ggplot 的图例不是为处理这种用例而设计的,我们可以通过将绘图(一个 ggplot2 对象)变成一个 grob 对象(本质上是一个嵌套的图形对象列表)来破解它,并在那里进行修改:
# convert original plot (saved as p) into a grob
g <- ggplotGrob(p)
找到与图例标签对应的嵌套 grob(有使用代码按关键字搜索的方法,但对于一次性用例,我发现通过列表查看更容易和更清晰...) :
> g # grob 15 (named guide-box) contains the legend
TableGrob (10 x 9) "layout": 18 grobs
z cells name grob
1 0 ( 1-10, 1- 9) background rect[plot.background..rect.174]
2 5 ( 5- 5, 3- 3) spacer zeroGrob[NULL]
3 7 ( 6- 6, 3- 3) axis-l absoluteGrob[GRID.absoluteGrob.124]
4 3 ( 7- 7, 3- 3) spacer zeroGrob[NULL]
5 6 ( 5- 5, 4- 4) axis-t zeroGrob[NULL]
6 1 ( 6- 6, 4- 4) panel gTree[panel-1.gTree.104]
7 9 ( 7- 7, 4- 4) axis-b absoluteGrob[GRID.absoluteGrob.117]
8 4 ( 5- 5, 5- 5) spacer zeroGrob[NULL]
9 8 ( 6- 6, 5- 5) axis-r zeroGrob[NULL]
10 2 ( 7- 7, 5- 5) spacer zeroGrob[NULL]
11 10 ( 4- 4, 4- 4) xlab-t zeroGrob[NULL]
12 11 ( 8- 8, 4- 4) xlab-b titleGrob[axis.title.x..titleGrob.107]
13 12 ( 6- 6, 2- 2) ylab-l titleGrob[axis.title.y..titleGrob.110]
14 13 ( 6- 6, 6- 6) ylab-r zeroGrob[NULL]
15 14 ( 6- 6, 8- 8) guide-box gtable[guide-box]
16 15 ( 3- 3, 4- 4) subtitle zeroGrob[plot.subtitle..zeroGrob.171]
17 16 ( 2- 2, 4- 4) title titleGrob[plot.title..titleGrob.170]
18 17 ( 9- 9, 4- 4) caption zeroGrob[plot.caption..zeroGrob.172]
> g$grobs[[15]] # grob 1 (named guides) contains the actual legend table
TableGrob (5 x 5) "guide-box": 2 grobs
z cells name grob
99_ff1a4629bd4c693e1303e4eecfb18bd2 1 (3-3,3-3) guides gtable[layout]
0 (2-4,2-4) legend.box.background zeroGrob[NULL]
> g$grobs[[15]]$grobs[[1]] # grobs 19-25 contain the legend labels
TableGrob (12 x 6) "layout": 26 grobs
z cells name grob
1 1 ( 1-12, 1- 6) background rect[legend.background..rect.167]
2 2 ( 2- 2, 2- 5) title zeroGrob[guide.title.zeroGrob.125]
3 3 ( 4- 4, 2- 2) key-3-1-bg rect[legend.key..rect.143]
4 4 ( 4- 4, 2- 2) key-3-1-1 segments[GRID.segments.144]
5 5 ( 5- 5, 2- 2) key-4-1-bg rect[legend.key..rect.146]
6 6 ( 5- 5, 2- 2) key-4-1-1 segments[GRID.segments.147]
7 7 ( 6- 6, 2- 2) key-5-1-bg rect[legend.key..rect.149]
8 8 ( 6- 6, 2- 2) key-5-1-1 segments[GRID.segments.150]
9 9 ( 7- 7, 2- 2) key-6-1-bg rect[legend.key..rect.152]
10 10 ( 7- 7, 2- 2) key-6-1-1 segments[GRID.segments.153]
11 11 ( 8- 8, 2- 2) key-7-1-bg rect[legend.key..rect.155]
12 12 ( 8- 8, 2- 2) key-7-1-1 segments[GRID.segments.156]
13 13 ( 9- 9, 2- 2) key-8-1-bg rect[legend.key..rect.158]
14 14 ( 9- 9, 2- 2) key-8-1-1 segments[GRID.segments.159]
15 15 (10-10, 2- 2) key-9-1-bg rect[legend.key..rect.161]
16 16 (10-10, 2- 2) key-9-1-1 segments[GRID.segments.162]
17 17 (11-11, 2- 2) key-10-1-bg rect[legend.key..rect.164]
18 18 (11-11, 2- 2) key-10-1-1 segments[GRID.segments.165]
19 19 ( 4- 4, 4- 4) label-3-3 text[guide.label.text.127]
20 20 ( 5- 5, 4- 4) label-4-3 text[guide.label.text.129]
21 21 ( 6- 6, 4- 4) label-5-3 text[guide.label.text.131]
22 22 ( 7- 7, 4- 4) label-6-3 text[guide.label.text.133]
23 23 ( 8- 8, 4- 4) label-7-3 text[guide.label.text.135]
24 24 ( 9- 9, 4- 4) label-8-3 text[guide.label.text.137]
25 25 (10-10, 4- 4) label-9-3 text[guide.label.text.139]
26 26 (11-11, 4- 4) label-10-3 text[guide.label.text.141]
因此我们可以找到对应于“Object”和“Lines”的grobs。它们是:
g$grobs[[15]]$grobs[[1]]$grobs[[19]] # label for "Object"
g$grobs[[15]]$grobs[[1]]$grobs[[24]] # label for "Lines"
> str(g$grobs[[15]]$grobs[[1]]$grobs[[19]]) # examine a label
List of 11
$ label : chr "Object"
$ x :Class 'unit' atomic [1:1] 0
.. ..- attr(*, "valid.unit")= int 0
.. ..- attr(*, "unit")= chr "npc"
$ y :Class 'unit' atomic [1:1] 0.5
.. ..- attr(*, "valid.unit")= int 0
.. ..- attr(*, "unit")= chr "npc"
$ just : chr "centre"
$ hjust : num 0
$ vjust : num 0.5
$ rot : num 0
$ check.overlap: logi FALSE
$ name : chr "guide.label.text.214"
$ gp :List of 5
..$ fontsize : num 8.8
..$ col : chr "black"
..$ fontfamily: chr ""
..$ lineheight: num 0.9
..$ font : Named int 1
.. ..- attr(*, "names")= chr "plain"
..- attr(*, "class")= chr "gpar"
$ vp : NULL
- attr(*, "class")= chr [1:3] "text" "grob" "gDesc"
我们可以看到格式是在 .$gp
下捕获的(图形参数列表,请参阅 here 了解更多信息)。我们可以制作一个更改列表,并在每个标签的原始列表中替换它们:
# make changes to format (examples of various things that can be changed)
gp.new <- list(fontsize = 10, # increase font size
col = "red", # change font color
font = 2L) # change from plain (1L) to bold (2L)
for(i in c(19, 24)){
gp <- g$grobs[[15]]$grobs[[1]]$grobs[[i]]$gp
ind1 <- match(names(gp.new), names(gp))
ind2 <- match(names(gp), names(gp.new))
ind2 <- ind2[!is.na(ind2)]
g$grobs[[15]]$grobs[[1]]$grobs[[i]]$gp <- replace(x = gp,
list = ind1,
values = gp.new[ind2])
}
rm(gp, gp.new, ind1, ind2, i)
绘制结果。请注意,要绘制 grob,您需要使用 grid 包中的 grid.draw()
:
grid::grid.draw(g)
关于r - 为一个ggplot2图创建两个图例并修改它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46683956/
前言: 有时候,一个数据库有多个帐号,包括数据库管理员,开发人员,运维支撑人员等,可能有很多帐号都有比较大的权限,例如DDL操作权限(创建,修改,删除存储过程,创建,修改,删除表等),账户多了,管理
所以我用 Create React App 创建并设置了一个大型 React 应用程序。最近我们开始使用 Storybook 来处理和创建组件。它很棒。但是,当我们尝试运行或构建应用程序时,我们不断遇
遵循我正在创建的控件的代码片段。这个控件用在不同的地方,变量也不同。 我正在尝试编写指令来清理代码,但在 {{}} 附近插入值时出现解析错误。 刚接触 Angular ,无法确定我错过了什么。请帮忙。
我正在尝试创建一个 image/jpeg jax-rs 提供程序类,它为我的基于 post rest 的 Web 服务创建一个图像。我无法制定请求来测试以下内容,最简单的测试方法是什么? @POST
我一直在 Windows 10 的模拟器中练习 c。后来我改用dev C++ IDE。当我在 C 中使用 FILE 时。创建的文件的名称为 test.txt ,而我给出了其他名称。请帮助解决它。 下面
当我们创建自定义 View 时,我们将 View 文件的所有者设置为自定义类,并使用 initWithFrame 或 initWithCode 对其进行实例化。 当我们创建 customUITable
我正在尝试为函数 * Producer 创建一个线程,但用于创建线程的行显示错误。我为这句话加了星标,但我无法弄清楚它出了什么问题...... #include #include #include
今天在做项目时,遇到了需要创建JavaScript对象的情况。所以Bing了一篇老外写的关于3种创建JavaScript对象的文章,看后跟着打了一遍代码。感觉方法挺好的,在这里与大家分享一下。 &
我正在阅读将查询字符串传递给 Amazon 的 S3 以进行身份验证的文档,但似乎无法理解 StringToSign 的创建和使用方式。我正在寻找一个具体示例来说明 (1) 如何构造 String
前言:我对 C# 中任务的底层实现不太了解,只了解它们的用法。为我在下面屠宰的任何东西道歉: 对于“我怎样才能开始一项任务但不等待它?”这个问题,我找不到一个好的答案。在 C# 中。更具体地说,即使任
我有一个由一些复杂的表达式生成的 ILookup。假设这是按姓氏查找人。 (在我们简单的世界模型中,姓氏在家庭中是唯一的) ILookup families; 现在我有两个对如何构建感兴趣的查询。 首
我试图创建一个 MSI,其中包含 和 exe。在 WIX 中使用了捆绑选项。这样做时出错。有人可以帮我解决这个问题。下面是代码: 错误 error LGH
在 Yii 中,Create 和 Update 通常使用相同的形式。因此,如果我在创建期间有电子邮件、密码、...other_fields...等字段,但我不想在更新期间专门显示电子邮件和密码字段,但
上周我一直在努力创建一个给定一行和一列的 QModelIndex。 或者,我会满足于在已经存在的 QModelIndex 中更改 row() 的值。 任何帮助,将不胜感激。 编辑: QModelInd
出于某种原因,这不起作用: const char * str_reset_command = "\r\nReset"; const char * str_config_command = "\r\nC
现在,我有以下由 original.df %.% group_by(Category) %.% tally() %.% arrange(desc(n)) 创建的 data.frame。 DF 5),
在今天之前,我使用/etc/vim/vimrc来配置我的vim设置。今天,我想到了创建.vimrc文件。所以,我用 touch .vimrc cat /etc/vim/vimrc > .vimrc 所
我可以创建一个 MKAnnotation,还是只读的?我有坐标,但我发现使用 setCooperative 手动创建 MKAnnotation 并不容易。 想法? 最佳答案 MKAnnotation
在以下代码中,第一个日志语句按预期显示小数,但第二个日志语句记录 NULL。我做错了什么? NSDictionary *entry = [[NSDictionary alloc] initWithOb
我正在使用与此类似的代码动态添加到数组; $arrayF[$f+1][$y][$x+1] = $value+1; 但是我在错误报告中收到了这个: undefined offset :1 问题:尝试创
我是一名优秀的程序员,十分优秀!