从莱迪思软件包R中更改点图中x轴标签的颜色

问题描述 投票:0回答:2

[我想知道是否有一种方法可以更改在R中使用点阵软件包创建的点图上某些x轴标签的颜色。

这是我的数据/代码的示例:

State <- factor(c("AZ", "AR", "NJ"))
Value <- c(1.2, 4.5, 2.0, 1.5, 4.0, 1.4)
Year <- c(2000, 2000, 2000, 2005, 2005, 2005)

p <- dotplot(Value ~ State, groups = Year, main = "Test Data",
        xlab = "State/Territory", ylab = "Data", auto.key = TRUE)

我希望将AZ和AR组合在一起(在x轴上为相同颜色的文本,我们可以将其设为蓝色)

我希望NJ成为它自己的组(在x轴文本中为不同的颜色,我们可以将其设为粉红色)

我还可以在图形中画一条垂直线以更好地划分组吗?

谢谢您的帮助!

r plot lattice dot
2个回答
1
投票

在传统绘图系统上进行控制非常复杂,如here所示。一种方法可能是使用像ggplot2这样的现代绘图系统。

library(ggplot2)
data <- data.frame(State = factor(c("AZ", "AR", "NJ")),Value = c(1.2, 4.5, 2.0, 1.5, 4.0, 1.4), Year = c(2000, 2000, 2000, 2005, 2005, 2005))

ggplot(data,aes(x=State,y=Value,color = State, shape = as.factor(Year))) + 
  geom_point(size = 3) + scale_color_manual(values=c("blue","blue","pink")) + scale_shape_discrete(name = "Year") +
  theme(axis.text.x = element_text(colour = c("blue","blue","pink")), plot.title = element_text(hjust = 0.5)) +
  geom_vline(xintercept = 2.5) + labs(title = "Test Data", x = "State/Territory", y = "Data") 

Plot


0
投票

好吧,只是为了好玩,有时候格子并不是那么复杂,并且在绘制一些大块图时仍然非常快。

在您的情况下,您需要将组指定为State,然后使用col来指定​​3种颜色,并在面板功能内执行panel.abline,例如:

dotplot(Value ~ State, groups=State,
main = "Test Data",
col=c("blue","blue","brown"),
panel = function(x, y,...) {
         panel.dotplot(x, y,levels.fos = unique(x),...)
         panel.abline(v=2.5)
       })

enter image description here

当然,缺点是您不能轻易指定另一个组变量,例如此处的year。

© www.soinside.com 2019 - 2024. All rights reserved.