Levelplot 用于分类数据?

huangapple go评论52阅读模式
英文:

Levelplot for categorical data?

问题

I want to produce a levelplot based on a matrix with the following code.

library("lattice")
x <- c(0:10)
y = seq(0,0.9,by=0.1)
z <- expand.grid(cost_hs, rr_ls)
val <- sample(c("A","B","C"),110,replace = T)
z$value = as.factor(val)
levelplot(value ~ Var1*Var2,data=z,main="")

My question is about the legend. The "value" is defined as categorical data, how to fix the legend as discrete label.

Levelplot 用于分类数据?

英文:

I want to produce a levelplot based on a matrix with the the following code.

library(&quot;lattice&quot;)
x&lt;-c(0:10)
y=seq(0,0.9,by=0.1)
z &lt;- expand.grid(cost_hs, rr_ls)
val&lt;-sample(c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;),110,replace = T)
z$value=as.factor(val)
levelplot(value ~ Var1*Var2,data=z,main=&quot;&quot;)

My question is about the legend. The "value" is defined as categorical data, how to fix the legend as discrete label.
Levelplot 用于分类数据?

答案1

得分: 0

levelplot 似乎不太适用于这种分类瓷砖图。您可以在 colorkey 参数中使用一些巧妙的技巧来获得类似的近似效果:

levelplot(value ~ Var1 * Var2, data = z, main = "", regions = TRUE,
          colorkey = list(at = c(0.5, 1, 1.5, 2, 2.5, 3, 3.5),
                          labels = c('', 'A', '', 'B', '', 'C', ''),
                          col = c('#ff80ff', '#ff80ff', 'white', 
                                  'white', '#80ffff', '#80ffff')))

我认为您可以使用 ggplot 获得更好的结果:

library(ggplot2)

ggplot(z, aes(Var1, Var2, fill = value)) +
  geom_tile() +
  scale_fill_manual(values = c(A = '#ff80ff', B = 'white', C = '#80ffff')) +
  theme_classic(base_size = 16) +
  coord_cartesian(expand = FALSE) +
  theme(panel.border = element_rect(fill = NA),
        axis.line = element_blank())

Levelplot 用于分类数据?

Levelplot 用于分类数据?

英文:

levelplot doesn't really seem to be designed for categorical tile plots like this. You can use some hacky tricks in the colorkey argument to get an approximation like this:

levelplot(value ~ Var1 * Var2, data = z, main = &quot;&quot;, regions = TRUE,
          colorkey = list(at = c(0.5, 1, 1.5, 2, 2.5, 3, 3.5),
                          labels = c(&#39;&#39;, &#39;A&#39;, &#39;&#39;, &#39;B&#39;, &#39;&#39;, &#39;C&#39;, &#39;&#39;),
                          col = c(&#39;#ff80ff&#39;, &#39;#ff80ff&#39;, &#39;white&#39;, 
                                  &#39;white&#39;, &#39;#80ffff&#39;, &#39;#80ffff&#39;)))

Levelplot 用于分类数据?

I think you would get a better result using ggplot:

library(ggplot2)

ggplot(z, aes(Var1, Var2, fill = value)) +
  geom_tile() +
  scale_fill_manual(values = c(A = &#39;#ff80ff&#39;, B = &#39;white&#39;, C = &#39;#80ffff&#39;)) +
  theme_classic(base_size = 16) +
  coord_cartesian(expand = FALSE) +
  theme(panel.border = element_rect(fill = NA),
        axis.line = element_blank())

Levelplot 用于分类数据?

huangapple
  • 本文由 发表于 2023年5月7日 20:35:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定