根据某一年的观察值对 ggplot2 中的面板数据进行排序
Ordering panel data in ggplot2 by the value of the observation in one certain year
我正在使用 ggplot2 绘制一个简单的数据面板。来自同一个人(区域)的观察来自两个不同的波浪,我想绘制我的图形,仅按其中一个波浪的值对个体进行排序。但是,ggplot 默认按两个波的平均值排序。这是 data.
的基本示例
data <- read.table(text = "
ID Country time Theil0
1 AT1 2004 0.10358155
2 AT2 2004 0.08181044
3 AT3 2004 0.08238252
4 BE1 2004 0.14754138
5 BE2 2004 0.07205898
6 BE3 2004 0.09522730
7 AT1 2010 0.10901556
8 AT2 2010 0.09593889
9 AT3 2010 0.07579683
10 BE1 2010 0.16500438
11 BE2 2010 0.08313131
12 BE3 2010 0.10281853
", sep = "", header = TRUE)
这里是剧情代码:
library(ggplot2)
pd <- position_dodge(0.4)
ggplot(data, aes(x=reorder(Country, Theil0), y=Theil0, colour = as.factor(time))) +
geom_point(size=3, position = pd)+
xlab("Region") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
ylab("Index") +
ggtitle("2004 and 2010")
以及由此产生的情节:
如您所见,仅按 2010 年的值(而不是这两年的平均值)排序将使 BE2 和 AT3 观测值切换顺序,这是我在图中更喜欢的顺序。感谢您对此的任何帮助。
我创建了一个使用通用 x
s 和 y
s 的可重现示例。基本上,您需要在因子上使用 ordered
函数:
x <- letters[1:4]
y1 <- 1:4
y2 <- c(1, 4, 2, 5) + 1
library(ggplot2)
library(reshape2) # used to melt the dummy dataset
df <- data.frame(x = x, y1 = y1, y2 = y2)
df2 <- melt(df, id.vars = "x", variable.name = "Group", value.name = "y")
df2$Group <- factor(df2$Group)
gg1 <- ggplot(data = df2, aes( x = x, y = y, color = Group)) +
geom_point()
ggsave("eample1.jpg", gg1, width = 3, height = 3)
给出与您所拥有的相似的情节:
但是,x
可能会重新排序:
df2$x2 <- ordered(df2$x, x[order(y2)])
gg2 <- ggplot(data = df2, aes( x = x2, y = y, color = Group)) +
geom_point()
ggsave("eample2.jpg", gg2, width = 3, height = 3)
得出这个数字:
另外,我经常被这个绊倒。我发现在 ggplot2
中调整级别有时是个骗局。
我正在使用 ggplot2 绘制一个简单的数据面板。来自同一个人(区域)的观察来自两个不同的波浪,我想绘制我的图形,仅按其中一个波浪的值对个体进行排序。但是,ggplot 默认按两个波的平均值排序。这是 data.
的基本示例data <- read.table(text = "
ID Country time Theil0
1 AT1 2004 0.10358155
2 AT2 2004 0.08181044
3 AT3 2004 0.08238252
4 BE1 2004 0.14754138
5 BE2 2004 0.07205898
6 BE3 2004 0.09522730
7 AT1 2010 0.10901556
8 AT2 2010 0.09593889
9 AT3 2010 0.07579683
10 BE1 2010 0.16500438
11 BE2 2010 0.08313131
12 BE3 2010 0.10281853
", sep = "", header = TRUE)
这里是剧情代码:
library(ggplot2)
pd <- position_dodge(0.4)
ggplot(data, aes(x=reorder(Country, Theil0), y=Theil0, colour = as.factor(time))) +
geom_point(size=3, position = pd)+
xlab("Region") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
ylab("Index") +
ggtitle("2004 and 2010")
以及由此产生的情节:
如您所见,仅按 2010 年的值(而不是这两年的平均值)排序将使 BE2 和 AT3 观测值切换顺序,这是我在图中更喜欢的顺序。感谢您对此的任何帮助。
我创建了一个使用通用 x
s 和 y
s 的可重现示例。基本上,您需要在因子上使用 ordered
函数:
x <- letters[1:4]
y1 <- 1:4
y2 <- c(1, 4, 2, 5) + 1
library(ggplot2)
library(reshape2) # used to melt the dummy dataset
df <- data.frame(x = x, y1 = y1, y2 = y2)
df2 <- melt(df, id.vars = "x", variable.name = "Group", value.name = "y")
df2$Group <- factor(df2$Group)
gg1 <- ggplot(data = df2, aes( x = x, y = y, color = Group)) +
geom_point()
ggsave("eample1.jpg", gg1, width = 3, height = 3)
给出与您所拥有的相似的情节:
但是,x
可能会重新排序:
df2$x2 <- ordered(df2$x, x[order(y2)])
gg2 <- ggplot(data = df2, aes( x = x2, y = y, color = Group)) +
geom_point()
ggsave("eample2.jpg", gg2, width = 3, height = 3)
得出这个数字:
另外,我经常被这个绊倒。我发现在 ggplot2
中调整级别有时是个骗局。