我试图制作一个条形图,其中最大的条最接近y轴,最短的条最远.所以这有点像我的表
Name Position
1 James Goalkeeper
2 Frank Goalkeeper
3 Jean Defense
4 Steve Defense
5 John Defense
6 Tim Striker
Run Code Online (Sandbox Code Playgroud)
所以我正在尝试建立一个条形图,根据位置显示玩家数量
p <- ggplot(theTable, aes(x = Position)) + geom_bar(binwidth = 1)
Run Code Online (Sandbox Code Playgroud)
但是图表显示守门员杆然后是防守,最后是前锋一个.我希望图表被排序,以便防守栏最接近y轴,守门员一个,最后是前锋一个.谢谢
我注意到在使用绘图创建的直方图中填充条形的默认值是反向字母顺序,而图例按字母顺序排序.我有什么方法可以按字母顺序排序吗?问题在下面的示例图中很明显.奖金问题:我如何将从左到右的条形顺序从字母顺序更改为减少计数总数?谢谢
df <- data.frame(
Site=c("A05","R17","R01","A05","R17","R01"),
Group=c("Fungia","Fungia","Acro","Acro","Porites","Porites"),
Count=c(6,8,6,7,2,9),
Total=c(13,10,15,13,10,15)
)
Site Group Count Total
1 A05 Fungia 6 13
2 R17 Fungia 8 10
3 R01 Acro 6 15
4 A05 Acro 7 13
5 R17 Porites 2 10
6 R01 Porites 9 15
qplot(df$Site,data=df,weight=df$Count,geom="histogram", fill=df$Group, ylim = c(0,16)) +
xlab("Sites") +
ylab("Counts") +
scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups")
Run Code Online (Sandbox Code Playgroud)

我试图将计数从高到低和填充颜色排序,以便它们匹配图例的字母顺序.我一直试图用初始帖子的提示调整它几个小时但没有成功.任何有关这方面的帮助将非常感谢!!!
有没有办法指定我希望堆积条形图的条形图与ggplot按照从最小到最大的四个因子的总和排序?(所以在下面的代码中,我想按所有变量的总和排序)我有一个数据帧中每个x值的总和,我融化了这个值以创建我构成图形的数据帧.
我用来绘制图表的代码是:
ggplot(md, aes(x=factor(fullname), fill=factor(variable))) + geom_bar()
Run Code Online (Sandbox Code Playgroud)
我当前的图表如下所示:
http://i.minus.com/i5lvxGAH0hZxE.png
最终的结果是我想要一个看起来有点像这样的图:
http://i.minus.com/kXpqozXuV0x6m.jpg
我的数据如下:
图表http://i.minus.com/izAmjF47yfsRQ.png中的数据
我把它融化成这种形式,每个学生都有每个类别的价值:
融化数据http://i.minus.com/i1rf5HSfcpzri.png
在使用以下行进行图表绘制之前
ggplot(data=md, aes(x=fullname, y=value, fill=variable), ordered=TRUE) + geom_bar()+ opts(axis.text.x=theme_text(angle=90))
Run Code Online (Sandbox Code Playgroud)
现在,我不确定我是否了解Chi的排序方式,以及是否可以将其应用于我所拥有的任何一帧的数据.也许在我拥有的原始数据框架中对数据进行排序是有帮助的,这是我首先展示的数据框架.
更新:我们想通了.请参阅此主题以获得答案: 在ggplot中订购堆积条形图
所以我有数据框
dput(df)
structure(list(Frequency = structure(c(1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L), .Label = c("2", "3", "4", "5"), class = "factor"), Prcentage = c(1,
33, 58, 8, 2, 40, 53, 5), label = list("Insufficient", "Average",
"Good", "Excellent", "Insufficient", "Average", "Good", "Excellent"),
name = c("implementation", "implementation", "implementation",
"implementation", "energy", "energy", "energy", "energy")), .Names = c("Frequency",
"Prcentage", "label", "name"), row.names = c(NA, 8L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
并使用以下代码
# Get the levels for type in the required order
df$label = factor(df$label, levels = …Run Code Online (Sandbox Code Playgroud) 我需要堆ETT由band每个ISO3,加上重新排序ISO3由总和-up的每个值ISO3。到目前为止,堆栈工作正常,但重新排序是问题所在。代码按单个ETT值(而不是值的总和)重新排序ETT。这是我的代码
library(dplyr)
library(ggplot2)
library(ggsci)
Ex4 <- example %>%
ggplot(aes(x = reorder(ISO3, ETT), y =ETT, fill = as.factor(band))) +
geom_bar(stat="identity")+theme_bw()+
guides(fill = guide_legend(nrow=3, title="Bands"))+
theme(legend.justification=c(1,0),legend.position = c(0.999,0.01), text=element_text(size=12))+
theme(axis.text.x = element_text(size=10),axis.text.y = element_text(size=7))+
coord_flip()+ scale_fill_igv()
Ex4
Run Code Online (Sandbox Code Playgroud)
这里可以下载数据(有想法)
这是预期的ISO3顺序(从第 5 个开始ISO3)
这是我迄今为止尝试过的
Tot <- example %>% group_by(ISO3) %>% summarise_all(.,funs(sum))
unmelt <- dcast(allc_b, ISO3 ~ band)
merge_2 <-merge(x = Tot, y = unmelt, …Run Code Online (Sandbox Code Playgroud)