ggplot2 geom_bar - 如何保持 data.frame 的顺序
ggplot2 geom_bar - how to keep order of data.frame
我对 geom_bar
中的数据顺序有疑问。
这是我的数据集:
SM_P,Spotted melanosis on palm,16.2
DM_P,Diffuse melanosis on palm,78.6
SM_T,Spotted melanosis on trunk,57.3
DM_T,Diffuse melanosis on trunk,20.6
LEU_M,Leuco melanosis,17
WB_M,Whole body melanosis,8.4
SK_P,Spotted keratosis on palm,35.4
DK_P,Diffuse keratosis on palm,23.5
SK_S,Spotted keratosis on sole,66
DK_S,Diffuse keratosis on sole,52.8
CH_BRON,Dorsal keratosis,39
LIV_EN,Chronic bronchities,6
DOR,Liver enlargement,2.4
CARCI,Carcinoma,1
我分配了以下列名:
colnames(df) <- c("abbr", "derma", "prevalence") # Assign row and column names
然后我绘制:
ggplot(data=df, aes(x=derma, y=prevalence)) + geom_bar(stat="identity") + coord_flip()
为什么 ggplot2 会随机更改我的数据顺序。我想让我的数据顺序与我的 data.frame
.
一致
非常感谢任何帮助!
作为答案发布,因为评论线程越来越长。您必须使用映射到 aes(x=...)
的变量的因子水平来指定顺序
# lock in factor level order
df$derma <- factor(df$derma, levels = df$derma)
# plot
ggplot(data=df, aes(x=derma, y=prevalence)) +
geom_bar(stat="identity") + coord_flip()
结果,与df
中的顺序相同:
# or, order by prevalence:
df$derma <- factor(df$derma, levels = df$derma[order(df$prevalence)])
相同的绘图命令给出:
我是这样读入数据的:
read.table(text=
"SM_P,Spotted melanosis on palm,16.2
DM_P,Diffuse melanosis on palm,78.6
SM_T,Spotted melanosis on trunk,57.3
DM_T,Diffuse melanosis on trunk,20.6
LEU_M,Leuco melanosis,17
WB_M,Whole body melanosis,8.4
SK_P,Spotted keratosis on palm,35.4
DK_P,Diffuse keratosis on palm,23.5
SK_S,Spotted keratosis on sole,66
DK_S,Diffuse keratosis on sole,52.8
CH_BRON,Dorsal keratosis,39
LIV_EN,Chronic bronchities,6
DOR,Liver enlargement,2.4
CARCI,Carcinoma,1", header=F, sep=',')
colnames(df) <- c("abbr", "derma", "prevalence") # Assign row and column names
我对 geom_bar
中的数据顺序有疑问。
这是我的数据集:
SM_P,Spotted melanosis on palm,16.2
DM_P,Diffuse melanosis on palm,78.6
SM_T,Spotted melanosis on trunk,57.3
DM_T,Diffuse melanosis on trunk,20.6
LEU_M,Leuco melanosis,17
WB_M,Whole body melanosis,8.4
SK_P,Spotted keratosis on palm,35.4
DK_P,Diffuse keratosis on palm,23.5
SK_S,Spotted keratosis on sole,66
DK_S,Diffuse keratosis on sole,52.8
CH_BRON,Dorsal keratosis,39
LIV_EN,Chronic bronchities,6
DOR,Liver enlargement,2.4
CARCI,Carcinoma,1
我分配了以下列名:
colnames(df) <- c("abbr", "derma", "prevalence") # Assign row and column names
然后我绘制:
ggplot(data=df, aes(x=derma, y=prevalence)) + geom_bar(stat="identity") + coord_flip()
为什么 ggplot2 会随机更改我的数据顺序。我想让我的数据顺序与我的 data.frame
.
非常感谢任何帮助!
作为答案发布,因为评论线程越来越长。您必须使用映射到 aes(x=...)
# lock in factor level order
df$derma <- factor(df$derma, levels = df$derma)
# plot
ggplot(data=df, aes(x=derma, y=prevalence)) +
geom_bar(stat="identity") + coord_flip()
结果,与df
中的顺序相同:
# or, order by prevalence:
df$derma <- factor(df$derma, levels = df$derma[order(df$prevalence)])
相同的绘图命令给出:
我是这样读入数据的:
read.table(text=
"SM_P,Spotted melanosis on palm,16.2
DM_P,Diffuse melanosis on palm,78.6
SM_T,Spotted melanosis on trunk,57.3
DM_T,Diffuse melanosis on trunk,20.6
LEU_M,Leuco melanosis,17
WB_M,Whole body melanosis,8.4
SK_P,Spotted keratosis on palm,35.4
DK_P,Diffuse keratosis on palm,23.5
SK_S,Spotted keratosis on sole,66
DK_S,Diffuse keratosis on sole,52.8
CH_BRON,Dorsal keratosis,39
LIV_EN,Chronic bronchities,6
DOR,Liver enlargement,2.4
CARCI,Carcinoma,1", header=F, sep=',')
colnames(df) <- c("abbr", "derma", "prevalence") # Assign row and column names