- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我以前曾帮助我向折线图添加一个点,结合 geom_point
与 geom_linerange
,创建我想要的确切效果(参见问题 here )。
带有 dataset is here 的 csv 文件.
但是,我想定义确切的形状geom_point
用于不同的组 - 目前他们都使用 pch = 21
, 这是一个根据 fill = time_period
取色的点.相反,我想为每个 time_period
成为不同的形状(本质上只突出特定时间段):
<10 years
应该有 pch = 32
10-20 years
应该有 pch = 1
>20 years
应该有 pch = 16
这是@aaumai 提供的工作图代码
library(tidyverse)
library(wesanderson)
df%>%
ggplot() +
geom_linerange( aes(ymin = minyear, ymax = maxyear,
colour = factor(diet),
x = fct_reorder(factor(id), desc(sort))),
size = 1) +
geom_point(aes(x = fct_reorder(factor(id), desc(sort)), y = max(maxyear) + 1, fill = time_period),
show.legend = TRUE, pch = 21, color = "white", size = 1)+
scale_colour_manual(values = wes_palette("Cavalcanti1")) +
labs(x = NULL, y = NULL) +
theme_bw() +
coord_flip() +
guides(colour = F) +
theme(panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(),
axis.ticks = element_blank(),
legend.position = "bottom",
panel.border = element_blank(),
legend.title = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.title = element_text(size = 20, vjust = 1, hjust = 0),
axis.text = element_text(size = 16),
axis.title = element_text(size = 20))
我已经尝试了其他 stackoverflow 帖子中建议的各种选项,但都没有奏效,包括:
[time_period == "<10 years"]
等等编辑:这是数据集的代码:
> dput(df)
structure(list(minyear = c(1970, 1980, 1983, 1980, 1974, 1974,
1974, 1974, 1974, 1988, 2000, 1981, 1974, 1975, 1974, 1974, 1981,
1981, 1981, 1981, 1981, 1993, 1990, 1982, 1982, 1982, 1982, 1982,
1982, 1982, 1982, 1982, 1982, 1982, 1978, 1974, 1974, 1974, 1974,
1974, 1974, 1974, 1974, 1981, 2001, 2001, 1994, 1994, 1994, 1994,
1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1990,
1994, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990,
1990, 1990, 1990, 1990, 1990, 1990, 2001, 1989, 1979, 1994, 1996,
1996, 1996, 1996, 1992, 1997, 1989, 1981, 1981, 1993, 1990, 1992,
1991, 1995, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 1988, 1981, 1981, 1982, 1982, 1970, 1981, 1981, 1981, 1982,
1970, 1998, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1977,
1977, 1977, 1977, 1977, 1977, 1995, 1995, 1996, 1981, 1996, 1996,
1996, 1996, 1996, 1996, 1996, 1982, 1982, 1985, 1977, 1977, 1977,
1977, 1977, 1977, 1999, 1983, 1982, 1982, 1982, 1989, 2004, 2004,
2004, 1996, 1996, 1996, 1996, 1996, 1984, 1972, 1972, 1975, 1983,
1983, 1983, 1983, 1983, 1983, 1987, 1987, 1987, 1987, 1979, 1993,
1999, 1999, 1989, 2004, 2004, 1970, 2004, 2001, 2001, 2001, 2001,
2001, 2001, 2001, 1992, 1994, 1994, 1980), maxyear = c(1980,
2008, 2008, 2008, 2007, 2008, 2008, 2008, 2008, 2000, 2006, 2000,
2011, 2012, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 1999, 2007,
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011,
2008, 2008, 2008, 2008, 2008, 2008, 2008, 2007, 2008, 1999, 2006,
2006, 2008, 2007, 2008, 2007, 2008, 2008, 2008, 2008, 2007, 2007,
2008, 2008, 2008, 2007, 2007, 2008, 2007, 2007, 2007, 2007, 2007,
2008, 2007, 2007, 2008, 2008, 2008, 2007, 2008, 2007, 2008, 2007,
2008, 2007, 1995, 2008, 2007, 2007, 2007, 2007, 2005, 2008, 2007,
2008, 2008, 2007, 2007, 2007, 2007, 2004, 2007, 2008, 2007, 2007,
2008, 2008, 2008, 2008, 2008, 2008, 2005, 2003, 2003, 2003, 2003,
2007, 2007, 2007, 2007, 2007, 1994, 2008, 2005, 2005, 2005, 2005,
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2007,
2007, 2008, 2009, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2008,
2008, 2005, 2006, 2006, 2006, 2006, 2006, 2006, 2007, 2000, 1999,
2000, 2000, 2007, 2011, 2011, 2011, 2008, 2008, 2008, 2008, 2008,
1989, 2007, 2007, 2007, 2006, 2006, 2006, 2006, 2006, 2006, 2003,
2003, 2003, 2003, 1998, 2008, 2006, 2006, 2007, 2010, 2010, 1986,
2010, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2007, 2007, 2007,
1988), id = structure(c(59L, 176L, 178L, 177L, 164L, 166L, 163L,
165L, 162L, 8L, 188L, 6L, 35L, 33L, 36L, 14L, 37L, 24L, 34L,
28L, 15L, 7L, 110L, 42L, 48L, 43L, 44L, 45L, 49L, 12L, 50L, 47L,
13L, 46L, 67L, 156L, 155L, 158L, 161L, 160L, 157L, 154L, 159L,
26L, 1L, 2L, 109L, 96L, 88L, 103L, 112L, 104L, 92L, 89L, 98L,
101L, 105L, 91L, 90L, 94L, 95L, 111L, 108L, 106L, 102L, 107L,
79L, 85L, 97L, 100L, 87L, 86L, 84L, 99L, 83L, 81L, 82L, 93L,
190L, 191L, 58L, 194L, 115L, 80L, 114L, 113L, 193L, 192L, 184L,
57L, 56L, 185L, 68L, 187L, 189L, 195L, 199L, 197L, 200L, 198L,
196L, 149L, 150L, 151L, 153L, 152L, 74L, 167L, 168L, 169L, 170L,
71L, 75L, 76L, 78L, 77L, 3L, 186L, 134L, 123L, 128L, 130L, 129L,
122L, 126L, 125L, 135L, 124L, 133L, 131L, 132L, 127L, 72L, 73L,
69L, 52L, 142L, 138L, 139L, 143L, 137L, 136L, 140L, 173L, 172L,
171L, 118L, 116L, 120L, 117L, 121L, 119L, 183L, 29L, 25L, 31L,
38L, 182L, 51L, 21L, 18L, 146L, 148L, 147L, 145L, 144L, 175L,
27L, 41L, 141L, 61L, 64L, 66L, 65L, 63L, 62L, 32L, 23L, 22L,
30L, 4L, 5L, 39L, 40L, 70L, 11L, 9L, 174L, 10L, 55L, 20L, 19L,
53L, 17L, 16L, 54L, 181L, 179L, 180L, 60L), .Label = c("10192",
"10193", "10415", "11751", "12015", "12016", "12019", "12022",
"12077", "12079", "12080", "12112", "12129", "12138", "12141",
"12169", "12171", "12175", "12176", "12178", "12180", "12249",
"12268", "12269", "12277", "12280", "12283", "12290", "12302",
"12348", "12355", "12362", "12363", "12364", "12369", "12377",
"12382", "12392", "12397", "12398", "12402", "12425", "12427",
"12431", "12432", "12437", "12440", "12444", "12447", "12451",
"12453", "12477", "12479", "12482", "12485", "12491", "12525",
"190", "2283", "5140", "6459", "6460", "6462", "6465", "6466",
"6467", "8157", "8550", "8562", "8563", "8564", "8572", "8574",
"8579", "8580", "8581", "8582", "8583", "8585", "8588", "8591",
"8592", "8593", "8594", "8599", "8600", "8601", "8605", "8606",
"8607", "8608", "8609", "8612", "8613", "8616", "8617", "8629",
"8630", "8631", "8649", "8650", "8651", "8652", "8653", "8655",
"8659", "8662", "8663", "8664", "8665", "8666", "8667", "8668",
"8669", "8677", "8683", "8685", "8687", "8689", "8691", "8692",
"8700", "8723", "8724", "8725", "8726", "8727", "8728", "8742",
"8743", "8761", "8763", "8764", "8765", "8766", "8767", "8772",
"8773", "8775", "8776", "8779", "8782", "8783", "8784", "8785",
"8786", "8787", "8788", "8793", "8794", "8795", "8797", "8799",
"8817", "8818", "8835", "8836", "8838", "8928", "8929", "8930",
"8938", "8939", "8940", "8941", "8942", "8943", "8944", "8948",
"8949", "8958", "8962", "8963", "8967", "8968", "8990", "8991",
"8992", "8993", "8994", "8995", "8999", "9000", "9001", "9012",
"9013", "9014", "9015", "9016", "9017", "9018", "9019", "9023",
"9024", "9086", "9541", "9542", "9543", "9544", "9545"), class = "factor"),
diet = structure(c(1L, 1L, 3L, 1L, 4L, 4L, 4L, 4L, 4L, 4L,
2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 4L,
4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 3L, 1L, 4L,
4L, 1L, 3L, 4L, 1L, 1L, 4L, 3L, 4L, 5L, 5L, 5L, 2L, 2L, 5L,
5L, 4L, 3L, 2L, 3L, 3L, 5L, 4L, 4L, 5L, 5L, 5L, 2L, 5L, 5L,
4L, 4L, 3L, 5L, 2L, 4L, 2L, 3L, 3L, 3L, 5L, 4L, 4L, 3L, 4L,
4L, 4L, 4L, 5L, 4L, 4L, 5L, 2L, 5L, 4L, 2L, 5L, 4L, 5L, 1L,
4L, 4L, 4L, 4L, 4L, 1L, 4L, 5L, 4L, 5L, 4L, 4L, 5L, 5L, 4L,
5L, 4L, 3L, 4L, 4L, 2L, 1L, 5L, 4L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 3L, 5L, 4L, 5L, 4L, 4L, 4L, 4L, 2L, 5L, 4L, 5L, 4L, 4L,
4L, 4L, 1L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 2L, 5L, 4L, 5L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
3L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 4L, 4L, 2L, 4L, 4L,
4L, 4L, 4L, 3L, 4L, 4L, 1L, 3L, 3L, 5L), .Label = c("VertFishScav",
"FruiNect", "Omnivore", "Invertebrate", "PlantSeed"), class = "factor"),
sort = c(11970, 11980, 31983, 11980, 41974, 41974, 41974,
41974, 41974, 41988, 22000, 41981, 41974, 41975, 41974, 41974,
41981, 41981, 41981, 41981, 41981, 41993, 51990, 41982, 41982,
41982, 41982, 41982, 41982, 31982, 41982, 41982, 41982, 41982,
11978, 41974, 41974, 31974, 11974, 41974, 41974, 11974, 31974,
41981, 12001, 12001, 41994, 31994, 41994, 51994, 51994, 51994,
21994, 21994, 51994, 51994, 41994, 31994, 21994, 31994, 31990,
51994, 41990, 41990, 51990, 51990, 51990, 21990, 51990, 51990,
41990, 41990, 31990, 51990, 21990, 41990, 21990, 31990, 32001,
31989, 51979, 41994, 41996, 31996, 41996, 41996, 41992, 41997,
51989, 41981, 41981, 51993, 21990, 51992, 41991, 21995, 52000,
42000, 52000, 12000, 42000, 42000, 42000, 42000, 42000, 12000,
41988, 51981, 41981, 51982, 41982, 41970, 51981, 51981, 41981,
51982, 41970, 31998, 41977, 41977, 21977, 11977, 51977, 41977,
31977, 41977, 41977, 41977, 41977, 41977, 41977, 31977, 51995,
41995, 51996, 41981, 41996, 41996, 41996, 21996, 51996, 41996,
51996, 41982, 41982, 41985, 41977, 11977, 31977, 41977, 41977,
41977, 41999, 41983, 41982, 41982, 41982, 41989, 42004, 42004,
42004, 41996, 21996, 51996, 41996, 51996, 31984, 41972, 41972,
41975, 41983, 41983, 41983, 41983, 41983, 31983, 41987, 41987,
41987, 41987, 41979, 31993, 41999, 41999, 31989, 42004, 42004,
21970, 42004, 42001, 42001, 42001, 42001, 32001, 42001, 42001,
11992, 31994, 31994, 51980), time_period = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("<10 years", "10-20 years", ">20 years"
), class = "factor")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -200L))
最佳答案
尝试在数据集中添加一个新列,根据 time_period 分配形状值,然后将 geom_point 的形状美学设置到该新列并添加 scale_shape_identity() 以便 ggplot 知道形状列的值已经代表美学
df %>%
mutate(shape =
case_when(
time_period == "<10 years" ~ 2,
time_period == "10-20 years" ~ 1,
time_period == ">20 years" ~ 16
)) %>%
ggplot() +
geom_linerange( aes(ymin = minyear, ymax = maxyear,
colour = factor(diet),
x = fct_reorder(factor(id), desc(sort))),
size = 1) +
geom_point(aes(x = fct_reorder(factor(id), desc(sort)),
y = max(maxyear) + 1,
shape = shape),
show.legend = TRUE, size = 1) +
scale_shape_identity() +
scale_colour_manual(values = wes_palette("Cavalcanti1")) +
labs(x = NULL, y = NULL) +
theme_bw() +
coord_flip() +
guides(colour = F)
获取图例的解决方法:
afafa %>%
ggplot() +
geom_linerange( aes(ymin = minyear, ymax = maxyear,
colour = factor(diet),
x = fct_reorder(factor(id), desc(sort))),
size = 1) +
geom_point(aes(x = fct_reorder(factor(id), desc(sort)),
y = max(maxyear) + 1,
shape = time_period),
show.legend = TRUE, size = 1) +
scale_shape_manual(values = c("<10 years" = 2, "10-20 years" = 1, ">20 years" = 16)) +
labs(x = NULL, y = NULL) +
theme_bw() +
coord_flip() +
guides(colour = F)
关于r - 定义在 ggplot2 中与多个 geom 一起使用时用于 geom_point 的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57216054/
在下面的查询中,为什么我们使用As foo (geom) 而不是As geom? foo() 做什么? SQL 查询 SELECT ST_SRID(geom) AS srid, ST_SRID(ST_
我有一组点,我想在 Java 中将其变成闭合的多边形。我目前正在尝试使用 java.awt.geom.Point2D 和 java.awt.geom.Area,但不知道如何将一组点变成一个区域。 我想
我正在编写一个偏移函数来测试移动物体是否接触空间中的其他可移动和固定物体。 为了测试这一点,我需要查看它们的边界矩形是否相交。 我想测试两个可移动物体是否像这样接触: table 车 - id:整数(
我尝试使用 ggplotly 函数将 ggplot geom-tile 图表转换为 plotly。然而,我意识到结果是不同的。请引用以下链接查看差异。除此之外,ggplotly 图表还缺少颜色条。请帮
在 ggplot2 – issue with overlay of lines and errorbars ,我提出了一个使用 lapply(.) 的答案以特定顺序生成几何图形组,以便每个级别的点/线
我试图在图中绘制两个“geom_vline()”。 下面的代码适用于一条垂直线: x=1:7 y=1:7 df1 = data.frame(x=x,y=y) vertical.lines <- c(2
我一直在阅读 vignette on extending ggplot2 ,但我对如何制作可以向绘图添加多个几何图形的单个几何图形有些困惑。 ggplot2 geoms 中已经存在多个几何图形,例如,
我在普林斯顿各个地点的交通流量数据框。 # dput(count) structure(list(intersection = structure(c(11L, 9L, 10L, 12L, 6L, 3
我在 postgres 数据库中有 geom。当我使用这个查询时 SELECT ST_AsGeoJSON(st_simplify(geom,0.01))::json As geometry FR
本文整理了Java中org.geolatte.geom.codec.WktDecoder类的一些代码示例,展示了WktDecoder类的具体用法。这些代码示例主要来源于Github/Stackover
本文整理了Java中org.geolatte.geom.codec.WkbEncoder类的一些代码示例,展示了WkbEncoder类的具体用法。这些代码示例主要来源于Github/Stackover
sf包似乎是一种比 sp 更易于用户使用的处理空间数据的方法。 .例如,如果我有一组纬度/经度坐标,我可以使用 ggplot2 的开发版本轻松绘图。 : library(sf) devtools::i
我想在使用stat_boxplot(geom = "errorbar")时更改晶须的线宽: set.seed(42) df <- data.frame(cond = factor( rep(c("A"
我有如下地理数据(但数据量大得多): library(tidyverse) library(gganimate) n % sort data % arrange(longitude) %>% muta
Hi I want to initialize an array of Point2D (Point2D.Double []) Somehow I fail, I get the following
根据 D3 文档,可以为 hull 方法分配自定义访问器来获取 x 和 y 坐标。 Hull documentation 我想使用这些自定义访问器,但我无法弄清楚语法。这就是我所做的,这基本上是一个不
我想使用 Java 的 Area 类 (java.awt.geom.Area) 对各种多边形进行减法和求交操作。 在许多这些情况下,减法操作可能会将源区域分成两部分。在这些情况下,我需要返回两个 Ar
我正在尝试在 R 中制作一个多面绘图线图,无论有多少组可用于分面,它都会生成一个图。当运行下面的代码时,我得到了一个理想的绘图,如下图所示 # R 3.6.0 # most of these pack
与其他 ggplot geom 相比,geom_vline 似乎在色彩美学方面“表现不佳”。我想弄清楚我是否对 geom_vline 有误解,或者这是否是对 geom_vline 设计的疏忽。 # F
我首先给你我的示例代码: x <- runif(1000,0, 5) y <- c(runif(500, 0, 2), runif(500, 3,5)) A <- data.frame("X"=x,"
我是一名优秀的程序员,十分优秀!