目的:
我试图在几个国家的城市中绘制一个数据图,按地区分组,然后由我自己的order
从顶部的1按照从y轴向下的图表的升序排列。
该图目前按反向字母顺序对区域进行分组;我希望它也按字母顺序排列。此外,它目前按字母顺序排列国家。我的order
未被使用。
到目前为止我尝试了什么:
您将看到下面已注释掉的两行代码:
#aspect.ratio=1/5, #I tried this and it did not work for me
和
#coord_fixed(ratio=1/500) + #new one for fixing y axis spacing #this did not solve it either
我尝试使用此处提出的解决方案解决此问题:https://www.r-bloggers.com/ordering-categories-within-ggplot2-facets-2/使用此代码:
group_by(rdatacities$region, rdatacities$country) %>%
arrange(desc(contribution)) %>%
ungroup() %>%
mutate(country = factor(paste(country, region, sep = "__"), levels = rev(paste(country, region, sep = "__")))) %>%
# --ggplot here--
scale_x_discrete(labels = function(x) gsub("__.+$", "", x))
但是group_by(rdatacities$region, rdatacities$country) %>%
会抛出此错误:
UseMethod(“group_by_”)中的错误: 没有适用于'group_by_'的方法适用于类“character”的对象
我不是R专家,我相信这是一个最小的,完整的,可验证的例子,但如果它是一堵可怕代码的墙,我会事先道歉。
library(readxl)
library(grid)
library(scales)
library(ggplot2)
library(graphics)
library(grDevices)
library(datasets)
p<-ggplot(rdatacities, aes(x=country, y=target, size=citiesorig)) +
geom_point(shape=21, alpha=.6, stroke=.75) + #municipal targets
facet_grid(region ~ ., scales = "free_y", space = "free_y") + #regional clusters
geom_point(data=rdatanation, shape=0, size=2.5, stroke=1, aes(colour='red'))
#national targets
plot <- p + theme(
plot.title = element_text(hjust=0.5), legend.key=element_rect(fill='white'),
panel.background = element_blank(), plot.margin = margin(2),
axis.ticks.y = element_blank(), panel.grid.major.y= element_line(size=.2, linetype='solid', colour='grey'),
axis.text.y = element_text(size=6),
panel.grid.major.x= element_blank(), axis.line.x = element_blank(),
axis.ticks.x=element_line(color='black'),
#aspect.ratio=1/5, #I tried this and it did not work for me
strip.background=element_rect(fill = NA, size = 0, color = "white", linetype = "blank")
) +
#coord_fixed(ratio=1/500) + #new one for fixing y axis spacing #this did not solve it either
scale_y_continuous(name="Target by 2030 as Percent of Base Year", labels = percent) +
scale_x_discrete(name="Country", expand=waiver()) +
coord_flip(ylim = c(0, 3)) +
ggtitle("National vs. Municipal GHG Emissions Targets") +
guides(colour = guide_legend(order = 1, "Targets"),
size = guide_legend(order = 2,"Municipal")) +
scale_color_manual(name="Targets", labels = c("National"), values = c('#843C0C'))
plot
以下是来自rdatacities的数据样本:
region country target cities order citiesorig
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 EAP JPN 0.660 1 71 1
2 EAP JPN 0.75 2 71 2
3 EAP JPN 0.8 1 71 1
4 EAP JPN 0.85 1 71 1
5 EAP JPN 0.88 1 71 1
6 EAP JPN 0.96 1 71 1
7 EAP KOR 0.6 1 72 1
8 ECA ALB 0.22 1 68 1
9 ECA AZE 0.2 1 65 1
10 ECA BLR 0.2 2 62 6
# ... with 391 more rows
以下是rdatanations
的数据示例region countryname country target EmBase EmCBase EmC2014 UrbPop2014 `%UrbPop2014` pop2014
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 EAP Australia AUS 1 263705. 15.5 15.4 20947819 0.893 23460694
2 EAP Brunei Darussalam BRN 2.09 6194. 23.9 22.1 316547 0.769 411704
3 EAP French Polynesia PYF NA 436. 2.20 2.92 154205 0.560 275484
4 EAP Japan JPN 0.82 1096180. 8.87 9.54 118393408 0.930 127276000
5 EAP Korea, Rep. KOR 0.74 246943. 5.76 11.6 41794948 0.824 50746659
6 EAP Malaysia MYS 5.93 56593. 3.14 8.03 22371755 0.740 30228017
7 EAP Mongolia MNG 1.89 9989. 4.57 7.13 2082457 0.712 2923896
8 EAP New Caledonia NCL NA 1584. 9.27 16.0 186697 0.697 268000
9 EAP New Zealand NZL 1.1 23546. 7.07 7.69 3889661 0.863 4509700
10 EAP Palau PLW 1.73 235. 15.6 12.3 18236 0.865 21094
# ... with 70 more rows