我遇到的问题是ggplot2没有在堆积条形图上绘制变量(水)。
以下是数据:
data <- structure(list(PARK_NON = structure(c(3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("apis", "indu", "miss",
"non_apis", "non_indu", "non_miss", "non_piro", "non_sacn", "non_slbe",
"piro", "sacn", "slbe"), class = "factor"), variable = structure(c(15L,
9L, 3L, 10L, 12L, 7L, 6L, 11L, 8L, 2L, 5L, 1L, 14L, 4L, 13L,
15L, 10L, 12L, 6L, 11L, 7L, 9L, 5L, 1L, 8L, 2L, 4L, 13L, 3L,
14L), .Label = c("Agriculture", "Barren land", "Developed - High intensity",
"Developed - Medium intensity", "Developed - Low intensity",
"Developed - Open space", "Evergreen forest", "Deciduous forest",
"Mixed forest", "Herbaceous", "Pasture", "Shrub", "Woody wetland",
"Herbaceous wetland", "Water"), class = "factor"), perc_veg = c(26.0239390821837,
0.0293350851750396, 6.90366110126389, 1.21719944965728, 1.57541802496374,
0.394990724328702, 5.82528684342088, 4.05485247757519, 16.4441745065715,
1.31842615202185, 9.09594225533093, 4.04411005201813, 4.73410430895216,
7.12470716561102, 11.213852770926, 7.66680361881418, 2.1247481894809,
1.30789300876845, 10.5308007720824, 12.723205663498, 0.713438751370985,
0.0478161985127231, 16.4578439049856, 11.5045071302907, 13.2759304844946,
0.0640865818499777, 10.2423233639193, 0.795200353968627, 5.43302045035016,
7.11238152761342), in_out = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Inside", "Outside"
), class = "factor")), class = "data.frame", row.names = c(NA,
-30L), .Names = c("PARK_NON", "variable", "perc_veg", "in_out"
))
我希望data$variable
以特定顺序堆叠,因此我定义了因子顺序...
data$variable <- factor(data$variable,
levels=c('Agriculture','Barren land','Developed - High intensity','Developed - Medium intensity','Developed - Low intensity',
'Developed - Open space','Evergreen forest','Deciduous forest','Mixed forest','Herbaceous','Pasture','Shrub',
'Woody wetland','Herbaceous wetland','Water'))
然后绘制......
library (ggplot2)
#colors to be used for NLCD classes
wa <- rgb(71/255,107/255,161/255,1) #water
do <- rgb(222/255,202/255,202/255,1) #developed-open
dl <- rgb(217/255,148/255,130/255,1) #developed-low density
dm <- rgb(238/255,0/255,0/255,1) #developed-medium density
dh <- rgb(171/255,0/255,0/255,1) #developed-high density
bl <- rgb(179/255,174/255,163/255,1) #barren land
df <- rgb(104/255,171/255,9/255,1) #deciduous forest
ef <- rgb(28/255,99/255,48/255,1) #evergreen forest
mf <- rgb(181/255,202/255,143/255,1) #mixed forest
sh <- rgb(204/255,186/255,125/255,1) #shrub/scrub
gr <- rgb(227/255,227/255,194/255,1) #grassland/herbaceous
pa <- rgb(220/255,217/255,61/255,1) #pasture/hay
ag <- rgb(171/255,112/255,40/255,1) #cultivated crops
ww <- rgb(186/255,217/255,235/255,1) #woody wetlands
hw <- rgb(112/255,163/255,186/255,1) #emergent/herbaceous wetlands
#map colors defined above to particular factors (levels) w/in the data, then use this vector for the color fill
nlcd.colors <- c("Water"=wa,"Developed - Open space"=do,"Developed - Low intensity"=dl,"Developed - Medium intensity"=dm,"Developed - High intensity"=dh,
"Barren land"=bl,"Deciduous forest"=df,"Evergreen forest"=ef,"Mixed forest"=mf,"Shrub"=sh,"Herbaceous"=gr,"Pasture"=pa,"Agriculture"=ag,
"Woody wetland"=ww,"Herbaceous wetland"=hw)
p <- ggplot () + geom_bar(data=data,aes(x=in_out,y=perc_veg,fill=variable,order=variable,width=0.6),stat='identity')
p <- p + scale_fill_manual("Vegetation type",values=nlcd.colors,
labels=c('Agriculture','Barren land','Developed - High intensity','Developed - Medium intensity','Developed - Low intensity',
'Developed - Open space','Evergreen forest','Deciduous forest','Mixed forest','Herbaceous','Pasture','Shrub',
'Woody wetland','Herbaceous wetland','Water'))
p <- p + guides(fill=guide_legend(reverse=TRUE))
p <- p + ylab('Percent of location')
p <- p + theme(axis.text=element_text(color="black"), axis.ticks.x = element_blank(), axis.title.x = element_blank())
p <- p + scale_y_continuous(expand=c(0,0),limits=c(0,100)) #to remove buffer on either side of 0 and 100
制作此图表....
传说中有“水”,但两条中没有。
有什么想法吗?
由于
-cherrytree
答案 0 :(得分:2)
所以当你包括:
scale_y_continuous(expand=c(0,0),limits=c(0,100))
ggplot没有足够的空间来适应最后一根酒吧。不过,这对我来说似乎有争议。将expand设置为0并设置限制似乎是一件合理的事情。如果您这样做:
scale_y_continuous(expand=c(0,0))
删除填充而不截断。这对我来说似乎是一个意想不到的结果。我还没有考虑过源代码,但我认为设置limits
在边界上有点过于严格。
答案 1 :(得分:2)
这是你最后一次致电
p <- p + scale_y_continuous(expand=c(0,0),limits=c(0,100))
这实际上是在处理原始数据,而不仅仅是将图形剪切为0和100
如果您仍想删除空白,可以尝试
p<-p+coord_cartesian(ylim=c(0,100)).
这只会在提供的y限制下剪切图表
另外,顺便说一下,退一步代码总是有帮助的。我觉得这在ggplot中特别有用。将其剥离回到获得类似图表所需的最小代码(即没有颜色格式和标签更改等)。然后,您将很快找到有问题的函数调用。然后更容易定位问题。