gpt4 book ai didi

r - 躲避离散的 y 值,geom_segment()

转载 作者:行者123 更新时间:2023-12-04 12:46:39 24 4
gpt4 key购买 nike

这是我的可重现示例:

IND.factions <- rbind(c("Mughal Empire", "IND", "1526-1-1", "1857-1-1", "IND"),
c("Maratha Empire", "IND", "1674-1-1", "1818-1-1", "IND"),
c("Vijayanagara Empire", "IND", "1336-1-1", "1646-1-1", "IND"),
c("Deccan Sultanates", "IND", "1527-1-1", "1686-1-1", "IND"),
c("Bahmani Sultanate", "IND", "1347-1-1", "1527-1-1", "IND"),
c("EIC", "IND", "1612-1-1", "1757-1-1", "ENG"),
c("Company Rule", "IND", "1757-1-1", "1858-1-1", "ENG"),
c("Maratha Empire", "IND", "1858-1-1", "1947-1-1", "ENG")
)

IND.factions <- data.frame(IND.factions, stringsAsFactors = FALSE)
names(IND.factions) <- c("Person", "Country", "StartDate", "EndDate", "Origin")
IND.factions$StartDate <- as.Date(IND.factions$StartDate, "%Y-%m-%d")
IND.factions$EndDate <- as.Date(IND.factions$EndDate, "%Y-%m-%d")

我想形象化的是类似于时间线的东西:
library(ggplot2)
p <- ggplot(data = IND.factions, aes(y = Country)) +
geom_segment(aes(x = StartDate, xend = EndDate, yend = Country, color = Origin), size = 10, position = position_dodge(width = 10))
p

我找不到避开重叠部分的解决方案。有没有人想到解决方法?
当然,我知道我可以将其分解为不同的因素,但这只是我的“最坏情况”解决方案

最佳答案

据我所知,geom_segment不允许闪避,但geom_linerange做。

library(ggplot2)

IND.factions <- rbind(c("Mughal Empire", "IND", "1526-1-1", "1857-1-1", "IND"),
c("Maratha Empire", "IND", "1674-1-1", "1818-1-1", "IND"),
c("Vijayanagara Empire", "IND", "1336-1-1", "1646-1-1", "IND"),
c("Deccan Sultanates", "IND", "1527-1-1", "1686-1-1", "IND"),
c("Bahmani Sultanate", "IND", "1347-1-1", "1527-1-1", "IND"),
c("EIC", "IND", "1612-1-1", "1757-1-1", "ENG"),
c("Company Rule", "IND", "1757-1-1", "1858-1-1", "ENG"),
c("Maratha Empire", "IND", "1858-1-1", "1947-1-1", "ENG")
)

IND.factions <- data.frame(IND.factions, stringsAsFactors = FALSE)
names(IND.factions) <- c("Person", "Country", "StartDate", "EndDate", "Origin")
IND.factions$StartDate <- as.Date(IND.factions$StartDate, "%Y-%m-%d")
IND.factions$EndDate <- as.Date(IND.factions$EndDate, "%Y-%m-%d")



ggplot(data = IND.factions, aes(x = Country, ymin = StartDate, ymax = EndDate,
color = Origin, group = Person)) +
geom_linerange(size = 10, position = position_dodge(.33)) +
coord_flip()

enter image description here

关于r - 躲避离散的 y 值,geom_segment(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38160528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com