R ggplot中的人口金字塔

R ggplot中的人口金字塔,r,ggplot2,R,Ggplot2,我读过一篇相关的文章(),但我有一个稍微不同的设置,这导致了一个混乱的金字塔 制作测试数据帧: test <- data.frame(cbind(c(replicate(3,"population 1"), replicate(3,"population 2")),c("top","middle","bottom","top","middle","bottom"),c(70,25,5,82,13,3))) 但这是错误的,我不明白为什么。顶部/中间/底部系数的顺序相反: 最后,我想做以下

我读过一篇相关的文章(),但我有一个稍微不同的设置,这导致了一个混乱的金字塔

制作测试数据帧:

test <- data.frame(cbind(c(replicate(3,"population 1"), replicate(3,"population 2")),c("top","middle","bottom","top","middle","bottom"),c(70,25,5,82,13,3)))
但这是错误的,我不明白为什么。顶部/中间/底部系数的顺序相反:

最后,我想做以下几点:

编辑-我通过在相反方向上明确施加因子重新排序(见下文)修复了单边块,但我仍然不理解为什么ggplot无法识别如何绘制数据,因此欢迎任何解释

# THIS PLOTS ONE SIDE OF THE PYRAMID CORRECTLY
testdf <- data.frame(cbind(c(replicate(3,"population 1"), replicate(3,"population 2")),c("Top","Middle","Bottom","Top","Middle","Bottom"),c(70,25,5,82,13,3)))
testdf$X3 <- factor(testdf$X3, levels=c(5,25,70,3,13,82))
testdf$X2 <- factor(testdf$X2, levels=c("Bottom","Middle","Top"))
g <- ggplot(data = testdf,  aes(x=X3, y=X2))
g <- g + geom_bar(data = subset(testdf, X1=="population 1") , stat = "identity")
g + coord_flip()
#这将正确绘制金字塔的一侧

testdf这应该可以让你开始了

test <- data.frame(
    X1 = c(replicate(3, "population 1"), replicate(3, "population 2")),
    X2 = c("top", "middle", "bottom", "top", "middle", "bottom"),
    X3 = c(70, 25, 5, 82, 13, 3)
)

test$X2 <- factor(test$X2, levels = c("bottom", "middle", "top"))

ggplot(data = test,  
       aes(x = X2, y = ifelse(X1 == "population 1", -X3, X3), fill = X1)) +
  geom_bar(stat = "identity") +
  coord_flip()

test这应该可以让你开始了

test <- data.frame(
    X1 = c(replicate(3, "population 1"), replicate(3, "population 2")),
    X2 = c("top", "middle", "bottom", "top", "middle", "bottom"),
    X3 = c(70, 25, 5, 82, 13, 3)
)

test$X2 <- factor(test$X2, levels = c("bottom", "middle", "top"))

ggplot(data = test,  
       aes(x = X2, y = ifelse(X1 == "population 1", -X3, X3), fill = X1)) +
  geom_bar(stat = "identity") +
  coord_flip()

test这对我有用:

test <-
  data.frame(
    X1 = c(replicate(3, "population 1"), replicate(3, "population 2")),
    X2 = c("top", "middle", "bottom", "top", "middle", "bottom"),
    X3 = c(70, 25, 5, 82, 13, 3)
  )
test$X3 <- with(test, ifelse(X1 == "population 1", -X3, X3))

library(ggplot2)
ggplot(data = test,  aes(x = X2, y = X3, fill = X1)) +
  geom_col() +
  coord_flip() +
  scale_y_continuous(labels = abs)

test这对我有用:

test <-
  data.frame(
    X1 = c(replicate(3, "population 1"), replicate(3, "population 2")),
    X2 = c("top", "middle", "bottom", "top", "middle", "bottom"),
    X3 = c(70, 25, 5, 82, 13, 3)
  )
test$X3 <- with(test, ifelse(X1 == "population 1", -X3, X3))

library(ggplot2)
ggplot(data = test,  aes(x = X2, y = X3, fill = X1)) +
  geom_col() +
  coord_flip() +
  scale_y_continuous(labels = abs)

test在使用上述帮助和以下指南后,将此作为答案发布,因为这是一个解决方案:

制作数据帧,
testdf
。将响应
testdf$percent
保留为数字而非系数:

testdf <- data.frame(population = c(replicate(3,"population 1"), replicate(3,"population 2")), 
                     layer =  c("Top","Middle","Bottom","Top","Middle","Bottom"), 
                     layernum = as.numeric(c(3,2,1,3,2,1)),
                     percent = as.numeric(c(70,25,5,82,13,3)))
testdf$percent <- ifelse(testdf$population == "population 1", -testdf$percent, testdf$percent)
制作情节:

g <- ggplot(data = testdf,  aes(x=layer, y=percent, fill=population))
g <- g + geom_bar(data = subset(testdf, population=="population 1") , stat = "identity")
g <- g + geom_bar(data = subset(testdf, population=="population 2") , stat = "identity")

g <- g + scale_y_continuous(breaks = seq(-100, 100, 25), 
                     labels = paste0(as.character(c(seq(100, 0, -25), seq(25, 100, 25))), "m"))
g+coord_flip()

g在使用上述帮助和以下指南后,将此作为答案发布,因为这是一个解决方案:

制作数据帧,
testdf
。将响应
testdf$percent
保留为数字而非系数:

testdf <- data.frame(population = c(replicate(3,"population 1"), replicate(3,"population 2")), 
                     layer =  c("Top","Middle","Bottom","Top","Middle","Bottom"), 
                     layernum = as.numeric(c(3,2,1,3,2,1)),
                     percent = as.numeric(c(70,25,5,82,13,3)))
testdf$percent <- ifelse(testdf$population == "population 1", -testdf$percent, testdf$percent)
制作情节:

g <- ggplot(data = testdf,  aes(x=layer, y=percent, fill=population))
g <- g + geom_bar(data = subset(testdf, population=="population 1") , stat = "identity")
g <- g + geom_bar(data = subset(testdf, population=="population 2") , stat = "identity")

g <- g + scale_y_continuous(breaks = seq(-100, 100, 25), 
                     labels = paste0(as.character(c(seq(100, 0, -25), seq(25, 100, 25))), "m"))
g+coord_flip()

g只是检查一下,…,你想让每边都有人口1吗?(因此,将值并排复制以使其对称?),或者这是一个输入错误,您希望金字塔的一侧有人口1,另一侧有人口2?最终,我希望一侧有人口1,另一侧有人口2。好的,然后,您可能应该避免使用子集方法,而是对总体设置条件以设置值的符号。也不应将值转换为因子。您使用的顺序与重新排序X2变量的顺序相反。(还要注意的是,您的代码没有正常工作,这也是因为在数据中,在对因子重新排序时,您对顶部、底部和中部使用小写,对第一个字符使用大写)。如果有帮助,请参阅下面的答案。但这可能很快就会被标记为重复。幸运的是,只是检查一下,…,你想要两边各有一个人口?(因此,将值并排复制以使其对称?),或者这是一个输入错误,您希望金字塔的一侧有人口1,另一侧有人口2?最终,我希望一侧有人口1,另一侧有人口2。好的,然后,您可能应该避免使用子集方法,而是对总体设置条件以设置值的符号。也不应将值转换为因子。您使用的顺序与重新排序X2变量的顺序相反。(还要注意的是,您的代码没有正常工作,这也是因为在数据中,在对因子重新排序时,您对顶部、底部和中部使用小写,对第一个字符使用大写)。如果有帮助,请参阅下面的答案。但这可能很快就会被标记为重复。幸运的是,这应该可以工作,但我不能使用geom_col()b/c我在ggplot2的1.0.0版中受到约束这应该可以工作,但我不能使用geom_col()b/c我在ggplot2的1.0.0版中受到约束