【问题标题】:Change axis label colors and legend line colors using xyplot and doubleYscale使用 xyplot 和 doubleYscale 更改轴标签颜色和图例线颜色
【发布时间】:2021-05-03 12:21:50
【问题描述】:

我正在尝试使用 latticeExtra 的 xyplot 和 doubleYscale 函数创建具有双 y 轴的图。如何将左右 y 轴的轴标签颜色以及图例线颜色更改为与图中的线条颜色相同?这些现在以蓝色和粉红色绘制,但应与下图中的线相同。请参阅下面的可重现代码示例和绘图:

library(lattice)
library(latticeExtra)

# Create the data frame.
My_DataFrame <- data.frame(
  emp_id = c (1:5), 
  salary = c(623.3,515.2,611.0,729.0,843.25), 
  weight = c(80,90,92,93,91),
  start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-05-11", "2014-11-15","2015-03-27")),
  stringsAsFactors = FALSE
)
# Print the data frame.         
print(My_DataFrame) 

obj1 <- xyplot(salary ~ start_date, My_DataFrame, type = "l", lwd = 2, col = "#6F99ADFF", col.lab = "black", ylim = seq(0,1000,100), scales = list(x=list(cex=1.2), y=list(cex=1.2)), xlab = list("", fontsize = 22), ylab = list("Hello", fontsize = 18), ylab.right = list("There", fontsize = 18))

obj2 <- xyplot(weight ~ start_date, My_DataFrame, type = "l", lwd = 2, col = "#E18727FF")

doubleYScale(obj1, obj2, col = c("#6F99ADFF","#E18727FF"), text = c("salary", "weight"), add.ylab2 = FALSE)

【问题讨论】:

    标签: r lattice


    【解决方案1】:

    添加 par.settings = list(superpose.line = list(col=c("#6F99ADFF", "#E18727FF"))), 在您的代码中:

    obj1 <- xyplot(salary ~ start_date, My_DataFrame, type = "l", lwd = 2, 
                   col = "#6F99ADFF", col.lab = "black", 
                   par.settings = list(superpose.line = list(col=c("#6F99ADFF", "#E18727FF"))),
                   ylim = seq(0,1000,100), 
                   scales = list(x=list(cex=1.2), 
                                 y=list(cex=1.2)), 
                   xlab = list("", fontsize = 22), 
                   ylab = list("Hello", fontsize = 18, col="#6F99ADFF"), ylab.right = list("There", fontsize = 18, col="#E18727FF"))
    
    obj2 <- xyplot(weight ~ start_date, My_DataFrame, type = "l", lwd = 2, col = "#E18727FF")
    
    doubleYScale(obj1, obj2, col = c("#6F99ADFF","#E18727FF"), text = c("salary", "weight"), add.ylab2 = F)
    

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 2018-01-10
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多