我正在尝试创建一个跨越多个地区的人口金字塔。问题在于这些区域的人口规模非常不同,从而在y轴上的比例尺上产生了问题(坐标翻转)。
我正在尝试遵循此处描述的方法:https://rpubs.com/walkerke/pyramids_ggplot2-通过对一个性别使用负数来构成金字塔,然后使用scale_y_continuous()
来消除负数。
首先,我为两个国家的2个年龄段的人创建了一个简单的样本数据集:
country <- c(1, 1, 1, 1, 2, 2, 2, 2)
age.range <- c("0-4", "0-4", "5-9", "5-9", "0-4", "0-4", "5-9", "5-9")
sex <- rep(c("M", "F"), times = 4)
pop <- c(-8, 9, -9, 8, -88, 99, -99, 88)
pop.pyr <- data.frame(country, age.range, sex, pop)
pop.pyr
country age.range sex pop
1 1 0-4 M -8
2 1 0-4 F 9
3 1 5-9 M -9
4 1 5-9 F 8
5 2 0-4 M -88
6 2 0-4 F 99
7 2 5-9 M -99
8 2 5-9 F 88
我可以按国家/地区构建人口金字塔和方面:
library(ggplot2)
ggplot(pop.pyr, aes(x = age.range, y = pop, fill = sex)) +
geom_col(data = subset(pop.pyr, sex == "M")) +
geom_col(data = subset(pop.pyr, sex == "F")) +
coord_flip() +
facet_wrap(~ country, scales = "free_x")
要在y刻度(coord_flip()
)上确定负数,我需要使用:scale_y_continuous()
;但是这样做意味着我必须为两个方面都选择一个scale_y_continuous()
,这是行不通的。
ggplot(pop.pyr, aes(x = age.range, y = pop, fill = sex)) +
geom_col(data = subset(pop.pyr, sex == "M")) +
geom_col(data = subset(pop.pyr, sex == "F")) +
coord_flip() +
facet_wrap(~ country, scales = "free_x") +
scale_y_continuous(breaks = seq(-100, 100, 20), labels = abs(seq(-100, 100, 20)))
唯一的解决方法是在by
中为seq
使用一个小变量,例如scale_y_continuous(breaks = seq(-100, 100, 2), labels = abs(seq(-100, 100, 2)))
。但是,这样做会使大范围混乱。
有没有一种方法可以设置scale_y_continuous()
,使得我可以在不同方面具有不同的比例,同时保持:scales = free_x
。否则,还有另一种方法可以使用scale_y_continuous()
以外的方法来消除金字塔中的负数。
如果不是,那么这样做是唯一的方法是分别显影每个图像,然后分别显影ggarrange()
或cowplot()
-本质上是手动刻面吗?
编辑:
我尝试根据评论使用facetscales()
,但是我无法使其按我想要的方式工作,也无法完全理解该手册文件。
使用此处的说明:https://github.com/zeehio/facetscales;我安装并加载了程序包
library(facetscales)
然后我创建秤列表:
scales.pyr <- list(`1` = scale_y_continuous(breaks = seq(-10, 10, 2), labels = abs(seq(-10, 10, 2))), `2` = scale_y_continuous(breaks = seq(-100, 100, 20), labels = abs(seq(-100, 100, 20))))
更新ggplot:
ggplot(pop.pyr, aes(x = age.range, y = pop, fill = sex)) +
geom_col(data = subset(pop.pyr, sex == "M")) +
geom_col(data = subset(pop.pyr, sex == "F")) +
coord_flip() +
facet_grid_sc(rows= vars(country), scales = list(y = scales.pyr))
这显然是不对的。手册文件(https://github.com/zeehio/facetscales/blob/master/man/facet_grid_sc.Rd)说我可以使用cols:
facet_grid_sc(行= NULL,cols = NULL,比例=“固定”,空格= “ fixed”,shrink = TRUE,labeller =“ label_value”,as.table = TRUE, switch = NULL,drop = TRUE,边距= FALSE,构面= NULL)
...
\ item {cols} {一组引用的变量或表达式 \ code {\ link [= vars] {vars()}}并在行上定义构面组 或列尺寸。可以命名变量(名称被传递 到\ code {labeller})。
如果我尝试cols:
ggplot(pop.pyr, aes(x = age.range, y = pop, fill = sex)) +
geom_col(data = subset(pop.pyr, sex == "M")) +
geom_col(data = subset(pop.pyr, sex == "F")) +
coord_flip() +
facet_grid_sc(cols= vars(country), scales = list(y = scales.pyr))
我得到:
Error in .subset2(x, i, exact = exact) :
attempt to select less than one element in get1index
也很清楚,比例尺是固定的,手册页上也说我可以使用scales = "free"
或折旧的"free_x"
。
\ item {scales} {两个元素(
x
和y
)的列表。每个元素都可以 为"fixed"
(在各个方面共享比例尺限制),"free"
(每个方面的限制有所不同)或命名列表(具有不同的限制) 每个方面值的比例。先前的比例值("fixed"
可以接受"free_x"
,"free_y"
,"free"
,但已弃用)。}
但是代码示例要求scales
参数用刻度列表填充。
最后,我真的很想在三个区域的两行中包含六个区域。手册页指示我可以使用行和列来表示不同的变量,但是对于单个变量,我看不到对nrow()
,ncol()
的任何引用。在较大的示例中使用它们可以得出:unused argument (ncol = 3)
。
答案 0 :(得分:1)
答案很简单:使用14:51:59.505 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.boot.enableautoconfiguration' in any propert
y source
14:51:59.505 [main] WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initiali
zation - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class
[org.sang.BlogserverApplication]; nested exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If
you are using a custom packaging, make sure that file is correct.
14:51:59.505 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.supp
ort.DefaultListableBeanFactory@271053e1: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.con
text.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.anno
tation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListene
rFactory,blogserverApplication,constantConfig,webSecurityConfig,adminController,userManaController,articleController,categoryController,loginRegController,userC
ontroller,articleService,categoryService,dataStatisticsComponent,userService]; root of factory hierarchy
14:51:59.520 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [org.sang.BlogserverApplication]; ne
sted exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, mak
e sure that file is correct.
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:556)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:185)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.ja
va:272)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at org.sang.BlogserverApplication.main(BlogserverApplication.java:12)
Caused by: java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure
that file is correct.
at org.springframework.util.Assert.notEmpty(Assert.java:277)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getCandidateConfigurations(AutoConfigurationImportSelector.java:153)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.selectImports(AutoConfigurationImportSelector.java:95)
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:547)
... 14 common frames omitted
。最终代码如下:
scale_y_continuous(labels = abs)
那是很长的路要走。谢谢@ Z.Lin。