繁体   English   中英

y轴上的点图组

[英]Dotplot groups on y-axis

我对R很dotplot ,对于在lattice软件包中使用dotplot函数有疑问。 我有按yearcommodity分组的多元数据。

Commodity   rank    Country year    tonnes
Coffee  1   Vietnam 2010    0.55
Coffee  2   Colombia    2010    0.75
Coffee  3   Brazil  2010    1.3
Coffee  4   Global Coffee   2010    2.6
Tea     5   Turkey  2010    0.2
Tea     6   Sri Lanka   2010    0.3
Tea     7   Kenya   2010    0.4
Tea     8   India   2010    1
Tea     9   China   2010    1.6
Tea     10  Global Tea  2010    3.5
Coffee  1   Vietnam 2009    0.6
Coffee  2   Brazil  2009    0.9
Coffee  3   Colombia    2009    0.9
Coffee  4   Global Coffee   2009    2.4
Tea     5   Turkey  2009    0.1
Tea     6   Sri Lanka   2009    0.1
Tea     7   Kenya   2009    0.5
Tea     8   India   2009    0.9
Tea     9   China   2009    1.7
Tea     10  Global Tea  2009    3.3

并且,这是它的dput()

structure(list(Commodity = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Coffee", 
"Tea"), class = "factor"), rank = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), Country = structure(c(10L, 
3L, 1L, 4L, 9L, 8L, 7L, 6L, 2L, 5L, 10L, 1L, 3L, 4L, 9L, 8L, 
7L, 6L, 2L, 5L), .Label = c("Brazil", "China", "Colombia", "Global Coffee", 
"Global Tea", "India", "Kenya", "Sri Lanka", "Turkey", "Vietnam"
), class = "factor"), year = c(2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L), tonnes = c(0.55, 0.75, 
1.3, 2.6, 0.2, 0.3, 0.4, 1, 1.6, 3.5, 0.6, 0.9, 0.9, 2.4, 0.1, 
0.1, 0.5, 0.9, 1.7, 3.3)), .Names = c("Commodity", "rank", "Country", 
"year", "tonnes"), class = "data.frame", row.names = c(NA, -20L
))

使用rank变量,我可以使用以下代码生成在y轴上具有特定顺序的点图

dotplot(reorder(Country,rank)~tonnes, data=commodity, pch=21, cex=1,groups=commodity$year, 
    main="Production by country" ,
    xlab="",gcolor="black")

我想按照Commodity在y轴上对变量进行分组,如下面链接中的示例所示。

IMG
(来源: statmethods.net

这个例子使用dotchart ,但我无法使用dotplot再现结果。 我将不胜感激。

如果您不介意使用ggplot2 ,那么这可能对您ggplot2

gg <- ggplot(data=dat, aes(x=tonnes, y=reorder(Country, rank)))
gg <- gg + geom_point(aes(color=Commodity))
gg <- gg + facet_wrap(~Commodity, ncol=1)
gg <- gg + labs(y="", title="Production by country")
gg <- gg + theme_bw()
gg <- gg + theme(legend.position="none")
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor.x=element_blank())
gg <- gg + theme(strip.background=element_blank())
gg

在此处输入图片说明

或者,您可以通过以下方式获得更多dotplot -ish:

gg <- ggplot(data=dat, aes(x=tonnes, y=reorder(Country, rank)))
gg <- gg + geom_point(aes(color=Commodity))
gg <- gg + facet_grid(Commodity~., scales="free", space="free")
gg <- gg + labs(y="", title="Production by country")
gg <- gg + theme_bw()
gg <- gg + theme(legend.position="none")
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor.x=element_blank())
gg <- gg + theme(strip.background=element_blank())
gg

在此处输入图片说明

要么:

gg <- ggplot(data=dat, aes(x=tonnes, y=reorder(Country, rank)))
gg <- gg + geom_point(aes(color=Commodity))
gg <- gg + facet_wrap(~Commodity, ncol=1, space="free")
gg <- gg + labs(y="", title="Production by country")
gg <- gg + theme_bw()
gg <- gg + theme(legend.position="none")
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor.x=element_blank())
gg <- gg + theme(strip.background=element_blank())
gg

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM