数据是这样的,请过目

[data]

names items quantity

edward earprotector 10

james earprotector 7

stone earprotector 7

alex earprotector 6

damon earprotector 4

judie earprotector 2

sheldon earprotector 2

simon earprotector 2

kevin earprotector 3

judy earprotector 1

july earprotector 9

edward footprotector 7

james footprotector 9

stone footprotector 6

alex footprotector 3

damon footprotector 2

judie footprotector 1

sheldon footprotector 4

simon footprotector 15

kevin footprotector 10

judy footprotector 2

july footprotector 1

edward gloves 2

james gloves 3

stone gloves 4

alex gloves 1

damon gloves 2

judie gloves 2

sheldon gloves 3

simon gloves 4

kevin gloves 3

judy gloves 4

july gloves 4

[/data]

已经用arrange函数排好序了,但是没有能够做出数据从大到小排列的分页图,这是为什么呢?

下面是我简陋的代码

<br />
> ggplot(data,aes(names,quantity,colour=factor(items))+<br />
     geom_point(size=3)+<br />
     facet_grid(~items,scales="free")<br />
</p>

回复 第1楼 的 Dearc:不知道LZ说的“数据从大到小排列”是具体指什么,但是我目测你ggplot最后,也就是factor((items))后面少了个括号...[s:12]

回复 第2楼 的 fanchong:按照原来的代码,出来的三幅图,以name为x轴都是按人名首字母排序的,不是我想要的按照数值从大到小决定x轴人名排序。[s:12]

问题的难点在于每个facet中x axis的order都不相同,所以基本排除可以用一个scale_x_*()或者说一个图实现的可能。

作为替代,你可以分别绘制三个ggplots p1, p2, p3 后,通过grid.arrange( p1, p2, p3 )显示。

library(gridExtra)

tmp <- subset(data, items == "earprotector", c(names, quantity))

tmp$names <- factor(tmp$names, levels = tmp[order(tmp$quantity), "names"])

p1 <- ggplot(tmp, aes(x = names, y = quantity)) +

geom_bar(stat = 'identity')

tmp <- subset(data, items == "footprotector", c(names, quantity))

tmp$names <- factor(tmp$names, levels = tmp[order(tmp$quantity), "names"])

p2 <- ggplot(tmp, aes(x = names, y = quantity)) +

geom_bar(stat = 'identity')

tmp <- subset(data, items == "gloves", c(names, quantity))

tmp$names <- factor(tmp$names, levels = tmp[order(tmp$quantity), "names"])

p3 <- ggplot(tmp, aes(x = names, y = quantity)) +

geom_bar(stat = 'identity')

grid.arrange( p1, p2, p3 )

其中test.txt是存储的你的data数据

<br />
library(ggplot2)<br />
library(plyr)<br />
data  <- read.table(file="test.txt",header = TRUE, stringsAsFactors = FALSE)<br />
data$n  <- as.numeric(factor(data$items))<br />
data = ddply(data,.(items,names),transform, x=paste(c(rep(' ',<br />
  n-1), names), collapse=''))<br />
data$x = factor(data$x, levels=data[order(data$quantity), 'x'])<br />
p = ggplot(data = data, aes(x = x, y = quantity))<br />
p = p + geom_bar(stat='identity')<br />
p = p + facet_grid(~items, scale='free_x')<br />
plot(p)<br />
</p>

回复 第5楼 的 Torenable:谢谢,照你的例子运行的一遍成功了,代码都能看懂

回复 第5楼 的 Torenable:

"reorder" function can change the levels of factor "names" according to the numerical sequence of "quantity" on the fly.

<br />
tmp <- subset(data, items == "earprotector", c(names, quantity))<br />
p1 <- ggplot(tmp, aes(x = reorder(names, quantity), y = quantity)) +<br />
  geom_bar(stat = 'identity')<br />
</p>

回复 第8楼 的 YSU:Good catch, thanks!

回复 第6楼 的 yiluheihei:先Mark一下

Another trick.

<br />
dat$names1 <- paste(dat$names, rep(1:nrow(dat)), sep="-")</p>
<p>p1 <- ggplot(dat, aes(x=reorder(names1, quantity), y=quantity))+<br />
  geom_bar(stat='identity')+<br />
  facet_grid(~items, scales="free")+<br />
  scale_x_discrete(breaks=dat$names1, labels=dat$names)</p>
<p>
</p>
2 个月 后
[未知用户]
看了N久终于明白为什么了。。。