Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot/geom_段:根据变量值对y轴标签(因子)进行排序_R_Ggplot2_Geom Text - Fatal编程技术网

R ggplot/geom_段:根据变量值对y轴标签(因子)进行排序

R ggplot/geom_段:根据变量值对y轴标签(因子)进行排序,r,ggplot2,geom-text,R,Ggplot2,Geom Text,下图是一个“伪”甘特图,旨在描绘一些战争的持续时间(日历年的x轴)加上伤亡人数。如果您能帮助我解决我面临的两个问题,我将不胜感激 1) 我想根据每个国家(分面军阀配置国家)内每个战争(geom_段)的开始日期(war.start)对y轴标签(变量/因子WarName)进行排序。我希望最早开始的战争在y轴的顶端;e、 g.对于苏丹,订单应为:第一南苏丹,第二南苏丹,苏丹人民解放军师,达尔富尔 我假设它与scale_y_离散(rev(levels(CoW.tmp$warocationcountry)

下图是一个“伪”甘特图,旨在描绘一些战争的持续时间(日历年的x轴)加上伤亡人数。如果您能帮助我解决我面临的两个问题,我将不胜感激

1) 我想根据每个国家(分面军阀配置国家)内每个战争(geom_段)的开始日期(war.start)对y轴标签(变量/因子WarName)进行排序。我希望最早开始的战争在y轴的顶端;e、 g.对于苏丹,订单应为:第一南苏丹,第二南苏丹,苏丹人民解放军师,达尔富尔

我假设它与scale_y_离散(rev(levels(CoW.tmp$warocationcountry))有关,但我不知道如何使它依赖于CoW.tmp$war.start

2) geom_text在geom_段旁边添加估计伤亡人数(总和死亡;数字);这些估计包括若干不适用/缺失数据。无论何时我将它们保留为NA,我都会收到错误消息:错误:“x”和“units”的长度必须大于0;我认为通过在geom_文本部分中添加na.rm=TRUE可以解决这个问题,但不幸的是事实并非如此

目前,丢失的数据用0编码。 CoW.tmp$sum.death[CoW.tmp$sum.death==0]类似这样的东西

library(ggplot2)
CoW.tmp <- with(CoW.tmp,CoW.tmp[order(WarLocationCountry,-as.integer(war.start)),])
CoW.tmp$WarName <- with(CoW.tmp,factor(WarName,levels=unique(WarName)))
ggplot(CoW.tmp) + 
  geom_segment(aes(color=WarType, x=war.start, xend=war.end, y=WarName, yend=WarName), size=1) +
  geom_point(aes(shape=Outcome2, color=WarType, x=war.end,y=WarName), size=3)+
  geom_point(aes(shape=WarType, color=WarType, x=war.start,y=WarName), size=3)+
  theme(plot.title=element_text(face="bold"),
        legend.position="bottom", 
        legend.title=element_text(size=7),
        legend.text=element_text(size=5),
        legend.box="vertical",
        axis.title.x = element_blank(),
        axis.text.x  = element_text(size=10),
        axis.title.y = element_blank(),
        axis.text.y  = element_text(size=10, face="bold"))+
  scale_color_discrete(name="War Type:",
                       breaks=c("4","5","6","7"),
                       labels=c("central control","local issues","regional internal","intercommunal"))+
  scale_shape_manual(values=c(1,3,4,5,6,7), name="Outcome:",
                     breaks=c("1","3","4","5","6","7"),
                     labels=c("victory", "compromise","transformed type of war","ongoing","stalemate","continues below war threshold"))+
  geom_text(aes(x=as.Date(conflict.end+1500), y=WarName, label=ifelse(sum.deaths!=0,sum.deaths,"")), size=3, na.rm=TRUE)+
  scale_x_date(limits = c(as.Date("1946-01-01"), as.Date("2010-01-01")))+
  ggtitle(paste("INTRA-STATE CONFLICTS (CoW)","","\n"))+
  facet_wrap(~WarLocationCountry, scales="free_y", ncol=1)

库(ggplot2)

要对级别(您的(1))进行排序,请不要尝试在
ggplot
中进行排序。它只是查看因子的
level
s的顺序。我认为,
reorder()
是基于另一个变量编辑的最简单方法。有关更多信息,请参见
?重新排序
。@Zoowwalk,您可以检查并获取
aes
调用中使用
reorder
的示例。您的示例没有运行-未定义
gtitle(…)
中的变量
a
。您可以使用调用
geom_text(…)
中的
label=ifelse(sum.death!=0,sum.death,“”)解决第二个问题。
CoW.tmp<-structure(list(conflict.end = structure(c(788, -2178, -1310, 
3648, 5921, 6569, 12793, 12793, 6496, 8881, 7695, 9609, 8354, 
9876, 9876, 9876, 9876, 9876, 9876, 9876, 11271, 11271, 11271, 
11271, 11271, 11271, 11271, 11271, 11271, 13493, 14041, 14041, 
14041, 14041), class = "Date"), WarType = structure(c(2L, 1L, 
2L, 1L, 2L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L
), .Label = c("4", "5", "7"), class = "factor"), war.start = structure(c(-2284, 
-2181, -1319, 1092, 3994, 4762, 5068, 8140, 6070, 6562, 6720, 
7751, 7909, 8382, 7988, 8382, 8382, 8382, 8382, 8382, 10263, 
10263, 10263, 10263, 11085, 11088, 11088, 11088, 11088, 12109, 
13520, 13213, 13430, 13440), class = "Date"), war.end = structure(c(788, 
-2178, -1310, 3648, 5921, 6569, 7908, 12793, 6496, 8881, 7695, 
9609, 8354, 9190, 9876, 9190, 9190, 9190, 8849, 9190, 10779, 
10779, 10779, 10779, 11271, 11271, 11271, 11271, 11271, 13493, 
13667, 14031, 14041, 14041), class = "Date"), WarName = c("First South Sudan", 
"Zanzibar Arab-African", "First Uganda", "Rhodesia", "Second Uganda", 
"Matabeleland", "Second South Sudan", "Second South Sudan", "Holy Spirit Movement", 
"Inkatha-ANC", "First Somalia", "First Sierra Leone", "The SPLA Division (Dinka-Nuer) War", 
"Second Somalia", "Second Somalia", "Second Somalia", "Second Somalia", 
"Second Somalia", "Second Somalia", "Second Somalia", "Second Sierra Leone", 
"Second Sierra Leone", "Second Sierra Leone", "Second Sierra Leone", 
"Second Sierra Leone", "Second Sierra Leone", "Second Sierra Leone", 
"Second Sierra Leone", "Second Sierra Leone", "Darfur", "Third Somalia", 
"Third Somalia", "Third Somalia", "Third Somalia"), Outcome2 = structure(c(3L, 
1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 6L, 1L, 1L, 7L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 1L, 1L, 
1L), .Label = c("1", "2", "3", "4", "5", "6", "7"), class = "factor"), 
    sum.deaths = c("0", "0", "0", "11000", "46000", "0", "0", 
    "0", "7000", "0", "0", "0", "0", "70", "70", "70", "70", 
    "70", "70", "70", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0"), WarLocationCountry = structure(c(4L, 
    6L, 5L, 7L, 5L, 7L, 4L, 4L, 5L, 3L, 2L, 1L, 4L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 2L, 
    2L, 2L, 2L), .Label = c("Sierra Leone", "Somalia", "South Africa", 
    "Sudan", "Uganda", "Zanzibar", "Zimbabwe"), class = "factor")), .Names = c("conflict.end", 
"WarType", "war.start", "war.end", "WarName", "Outcome2", "sum.deaths", 
"WarLocationCountry"), class = "data.frame", row.names = c(34L, 
39L, 44L, 67L, 114L, 120L, 127L, 128L, 134L, 136L, 138L, 152L, 
155L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 197L, 198L, 199L, 
200L, 201L, 202L, 203L, 204L, 205L, 237L, 246L, 247L, 248L, 249L
))
library(ggplot2)
CoW.tmp <- with(CoW.tmp,CoW.tmp[order(WarLocationCountry,-as.integer(war.start)),])
CoW.tmp$WarName <- with(CoW.tmp,factor(WarName,levels=unique(WarName)))
ggplot(CoW.tmp) + 
  geom_segment(aes(color=WarType, x=war.start, xend=war.end, y=WarName, yend=WarName), size=1) +
  geom_point(aes(shape=Outcome2, color=WarType, x=war.end,y=WarName), size=3)+
  geom_point(aes(shape=WarType, color=WarType, x=war.start,y=WarName), size=3)+
  theme(plot.title=element_text(face="bold"),
        legend.position="bottom", 
        legend.title=element_text(size=7),
        legend.text=element_text(size=5),
        legend.box="vertical",
        axis.title.x = element_blank(),
        axis.text.x  = element_text(size=10),
        axis.title.y = element_blank(),
        axis.text.y  = element_text(size=10, face="bold"))+
  scale_color_discrete(name="War Type:",
                       breaks=c("4","5","6","7"),
                       labels=c("central control","local issues","regional internal","intercommunal"))+
  scale_shape_manual(values=c(1,3,4,5,6,7), name="Outcome:",
                     breaks=c("1","3","4","5","6","7"),
                     labels=c("victory", "compromise","transformed type of war","ongoing","stalemate","continues below war threshold"))+
  geom_text(aes(x=as.Date(conflict.end+1500), y=WarName, label=ifelse(sum.deaths!=0,sum.deaths,"")), size=3, na.rm=TRUE)+
  scale_x_date(limits = c(as.Date("1946-01-01"), as.Date("2010-01-01")))+
  ggtitle(paste("INTRA-STATE CONFLICTS (CoW)","","\n"))+
  facet_wrap(~WarLocationCountry, scales="free_y", ncol=1)