r - 我需要在情节上标出最大的不同
问题描述
第一件事是……对不起我的英语。
我有两个不同的对象:一个因子和一个数字。我用和打印一个geom_point()
和。geom_line()
x=Year with trimester (As factor)
y=value (numeric)
我将y
信息分为两个因素级别,通过group= a factor variable
.
然后,我有两条线。我需要在两条线之间最大差异和最小差异的位置打印 3 条垂直线。
我已经看到我可以用geom_segment
or做一条线geom_line
。但我需要开始和结束,所以我需要x
参考。但我x
是一个因素,而不是一个数字。
那么,我能做些什么呢?
我在这里 输入图像描述
我需要这样的东西(第 3 页,第一张图):
https://riull.ull.es/xmlui/bitstream/handle/915/6574/A_08_%282017%29_07.pdf?sequence=1&isAllowed=y
这是我用melt
函数转换它之前的数据(前 20 行)(通过“Ambos.Sexos”)
`
structure(list(AMBOS.SEXOS = structure(20:1, .Label = c("2014TII",
"2014TIII", "2014TIV", "2015TI", "2015TII", "2015TIII", "2015TIV",
"2016TI", "2016TII", "2016TIII", "2016TIV", "2017TI", "2017TII",
"2017TIII", "2017TIV", "2018TI", "2018TII", "2018TIII", "2018TIV",
"2019TI"), class = "factor"), Activos = structure(c(18L, 20L,
19L, 12L, 11L, 17L, 4L, 5L, 2L, 7L, 10L, 8L, 6L, 13L, 14L, 15L,
16L, 9L, 1L, 3L), .Label = c("1.086,16", "1.089,43", "1.091,95",
"1.094,48", "1.094,62", "1.097,06", "1.100,27", "1.100,74", "1.100,83",
"1.102,15", "1.107,86", "1.108,98", "1.110,40", "1.110,65", "1.110,78",
"1.114,98", "1.118,25", "1.130,20", "1.131,53", "1.141,58"), class = "factor"),
Ocupados = structure(c(18L, 20L, 19L, 17L, 16L, 15L, 14L,
13L, 8L, 12L, 11L, 7L, 9L, 10L, 6L, 5L, 4L, 3L, 1L, 2L), .Label = c("723,87",
"735,09", "758,67", "771,46", "774,24", "793,48", "799,91",
"809,66", "811,85", "813,34", "815,45", "826,28", "828,61",
"855,17", "871,81", "879,46", "886,57", "892,47", "909,26",
"913,36"), class = "factor")), row.names = c(NA, 20L), class = "data.frame")
`
解决方案
这就是你想要的。看起来你可能需要一些str_replace
,这是一种将这样写的数字转换1.000,50
为我当地数字标准的快速而肮脏的方法1000.50
。
我命名了你的data.frame
qq
. 首先计算差异。
qq <- qq %>% mutate(
Activos = str_replace(Activos, "\\.", ""),
Activos = str_replace(Activos, ",", "\\."),
Ocupados = str_replace(Ocupados, "\\.", ""),
Ocupados = str_replace(Ocupados, ",", "\\."),
Activos = as.numeric(Activos),
Ocupados = as.numeric(Ocupados),
diferencia = Activos - Ocupados,
# create false x axis for plotting purposes
# double check, looks like your data is ordered
# with the most recent first, we will need to account for that
falso_x = -desc(as.numeric(AMBOS.SEXOS)))
minimo <- qq %>% arrange(diferencia) %>%
head(n=1)
maximo <- qq %>% arrange(desc(diferencia)) %>%
head(n=1)
现在制作情节。我没有走这reshape::melt
条路。尽管有可能,但在您的情况下可能会更麻烦。(您可以尝试在制作绘图之前reshape::melt(qq, id.vars=c("AMBOS.SEXOS", "falso_x")
过滤掉这些值)。diferencia
诀窍是使用错误的 x 轴,然后手动放置标签。
qq %>%
ggplot(aes(falso_x, Activos))+
geom_point()+
geom_line()+
geom_point(aes(falso_x, Ocupados))+
geom_line(aes(falso_x, Ocupados))+
scale_x_continuous(breaks=1:max(qq$falso_x), #addapt here for length,
labels=rev(qq$AMBOS.SEXOS))+
xlab("") + ylab("") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
geom_segment(data=minimo, aes(x=falso_x, xend=falso_x,
y=Ocupados, yend=Activos), color="blue")+
geom_segment(data=maximo, aes(x=falso_x, xend=falso_x,
y=Ocupados, yend=Activos), color="red")
更新
创造传奇,ggplot2
真的很喜欢里面有东西aes
。我们可以做一个变通方法并命名一个假颜色,它会被分配给一个真实的颜色scale_color_manual
。还要检查这个答案。
这主要是一个黑客。正如我在上面所说,如果你走这reshape2::melt
条路,你可以有其他选择(见下文)。
qq %>%
ggplot(aes(falso_x, Activos))+
geom_point(aes(color="Activo"))+
geom_line(aes(color="Activo"))+
geom_point(aes(falso_x, Ocupados, color="Ocupado"))+
geom_line(aes(falso_x, Ocupados, color="Ocupado"))+
scale_x_continuous(breaks=1:max(qq$falso_x), #addapt here for length,
labels=rev(qq$AMBOS.SEXOS))+
xlab("") + ylab("") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
geom_segment(data=minimo, aes(x=falso_x, xend=falso_x,
y=Ocupados, yend=Activos), color="blue")+
geom_segment(data=maximo, aes(x=falso_x, xend=falso_x,
y=Ocupados, yend=Activos), color="red")+
scale_colour_manual(name="Grupo",
values=c(Ocupado="darkorange",
Activo="green"))
reshape2::melt
方法
m <- reshape2::melt(qq, id.vars=c("AMBOS.SEXOS","falso_x"))
m %>% filter(variable!="diferencia") %>%
ggplot(aes(falso_x, value, color=variable))+
geom_point()+
geom_line()+
scale_x_continuous(breaks=1:max(qq$falso_x), #addapt here for length,
labels=rev(qq$AMBOS.SEXOS))+
xlab("") + ylab("") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
geom_segment(data=minimo, aes(x=falso_x, xend=falso_x,
y=Ocupados, yend=Activos), color="blue")+
geom_segment(data=maximo, aes(x=falso_x, xend=falso_x,
y=Ocupados, yend=Activos), color="red")
推荐阅读
- java - Java中两个String对象之间使用+运算符时的实现是什么?
- android - 图像未显示在 Imageview 中(在 Fragent 内,在选项卡式活动中的 ViewPager 内)-毕加索未替换占位符
- sorting - 按 Racket 列表中的元素排序
- python - 如何在pygame中添加主菜单和重试菜单
- sharepoint - 用于在 SharePoint PnP 搜索中获取用户名的用户句柄表达式
- node.js - Redis 在 docker 中未定义
- javascript - 我们如何在另一个组件中创建抽屉导航并在 app.js 中调用它
- python - Google Foobar Challenge(Python) - 幸运羊问题
- gwt - 在 GWT 中继承自定义 jar 文件不起作用
- r - 绘制大型邻接矩阵(不是图)的最有效方法是什么?