[英]R lattice — unexpected legend
I want to graphically compare carbon pools under different forms of land use.我想以图形方式比较不同土地利用形式下的碳库。 I am reading in my data, later I remove a factor level.
我正在阅读我的数据,后来我删除了一个因子水平。
However, when I display the data the legend does not work.但是,当我显示数据时,图例不起作用。 The first legend symbol has no label, and the deleted factor (SPEZIALKULTUREN) is still shown.
第一个图例符号没有标签,删除的因子 (SPEZIALKULTUREN) 仍然显示。
how can I control the legend?我怎样才能控制传奇?
A sample of the data is shown below enter image description here数据示例如下所示 在此处输入图像描述
enter image description here在此处输入图片说明
agdata <- read.csv("C:/Jandl/LfdProjekte/2017FAO/2020Paper/Agdata.csv", header = FALSE, sep = ";", dec = ",")
colnames(agdata) <- c("State","AgType","Cpool")
agdata$Cpool <- as.numeric(agdata$Cpool)
levels(agdata$AgType)[levels(agdata$AgType)=="ACKERLAND"] <- "crop"
levels(agdata$AgType)[levels(agdata$AgType)=="ALMEN"] <- "alpine meadow"
levels(agdata$AgType)[levels(agdata$AgType)=="EXTENSIVES GRÜNLAND *"] <- "extens grassland"
levels(agdata$AgType)[levels(agdata$AgType)=="INTENSIVES GRÜNLAND**"] <- "intens grassland"
levels(agdata$AgType)[levels(agdata$AgType)=="WEINGARTEN***"] <- "vineyard"
#dropping SPEZIALKULTUREN
agdata <- subset(agdata,agdata$AgType !="SPEZIALKULTUREN")
agdata <- subset(agdata,agdata$State !="")
agdata <- subset(agdata,agdata$State !="Wien")
library(lattice)
colors = c("lightsalmon3", "lightgoldenrod2", "cadetblue4", "yellow", "red", "blue")
barchart(
data = agdata,
origin = 0,
Cpool ~ State,
groups = AgType ,
xlab = list (
label = "State",
font = 2,
cex = 1),
ylab= list (
label = "C pool t/ha",
font = 2,
cex = 1),
#ylim=c(0,25),
labels = TRUE,
auto.key = list(space="top", columns= 3),
par.settings = list(superpose.polygon = list(col = colors)))
I am not sure, but it happened to me sometimes that, when using the subset function, removed factors remain empty in the dataframe and will occasionally show on some plots.我不确定,但有时发生在我身上,当使用子集函数时,删除的因子在数据框中仍然是空的,并且偶尔会显示在一些图上。 For example, consider this simple dataset named "myexample":
例如,考虑这个名为“myexample”的简单数据集:
> myexample
var1 var2
1 1 a
2 5 a
3 6 b
4 3 b
5 7 c
Now I can subset it to keep only rows where var2 is either a or b现在我可以将其子集化以仅保留 var2 为 a 或 b 的行
> myexample2<-subset(myexample, var2=="a" | var2=="b")
> myexample2
var1 var2
1 1 a
2 5 a
3 6 b
4 3 b
> levels(myexample2$var2)
[1] "a" "b" "c"
It will look like "c" was dropped, but it is still there.它看起来像“c”被删除了,但它仍然存在。 To properly get rid of it, you can use the function droplevels()
要正确摆脱它,您可以使用函数droplevels()
> myexample2<-droplevels(myexample2)
> myexample2
var1 var2
1 1 a
2 5 a
3 6 b
4 3 b
> levels(myexample2$var2)
[1] "a" "b"
And now it is really gone.而现在它真的消失了。 Maybe give it a try and see if at least the removed factor is no longer in the plot.
也许试一试,看看至少删除的因素是否不再出现在情节中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.