【问题标题】:How to add months and years instead of numbers in lattice dot plot?如何在格点图中添加月份和年份而不是数字?
【发布时间】:2019-10-19 11:04:26
【问题描述】:

我对 R 比较陌生,每当我尝试在我的点图中添加月份和年份时都会遇到问题。当我使用 lattice 运行数据时,我会这样做:

require(lattice)
data_conrad = read.csv("/Users/Danniel/Desktop/conrad_made_up.csv", header = TRUE)
data_conrad
lattice::dotplot(data_conrad$Patient ~ data_conrad$Value | data_conrad$Year, 
        data=data_conrad, xlab="Time", ylab="Patient", scales= list(x = list(at = seq(from = 1, to = 12, by =1))))

但是,我试图获取实际月份而不是 (1,2,3,4...12),并且我试图用实际年份 (2010, 2011, 2012) 替换 "Data_Conrad$Year" ),但是我遇到了问题。

输入dput(data_conrad) 后,R 的输出如下:

structure(list(Patient = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 1L, 6L, 
8L), .Label = c(" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", 
" 9", "10"), class = "factor"), Month = structure(c(5L, 4L, 6L, 
8L, 1L, 3L, 2L, 2L, 4L, 10L, 1L, 7L, 11L, 10L, 9L, 10L, 10L, 
3L, 5L, 6L, 3L, 5L), .Label = c("Apr", "Aug", "Dec", "Feb", "Jan", 
"Jul", "Jun", "Mar", "May", "Nov", "Sep"), class = "factor"), 
    Year = structure(c(1L, 2L, 3L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 
    2L, 2L, 3L, 1L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L), .Label = c("2010", 
    "2011", "2012"), class = "factor"), Value = structure(c(1L, 
    2L, 7L, 3L, 4L, 11L, 8L, 8L, 2L, 10L, 4L, 6L, 9L, 10L, 5L, 
    10L, 10L, 11L, 1L, 7L, 11L, 1L), .Label = c(" 1", " 2", " 3", 
    " 4", " 5", " 6", " 7", " 8", " 9", "11", "12"), class = "factor")), class = "data.frame", row.names = c(NA, 
-22L))

非常感谢您的帮助!

【问题讨论】:

  • 我对@9​​87654326@ 不太熟悉,但如果您提供示例数据来使用它可能会有所帮助。它可以来自dput(head(data_conrad)) 或以编程方式构建,例如data.frame(Year = ..., ...)
  • 绝对!我刚刚使用 'dput(data_conrad)' 编辑了帖子,非常感谢!

标签: r lattice


【解决方案1】:

使用 labels 参数定义 X 轴标签可以解决您的问题。

您可以使用以下代码:

require(lattice)

lattice::dotplot(Patient ~ Value | Year, 
                 data = data_conrad, xlab = "Time", ylab = "Patient" , 
                 scales= list(x = list(at = seq(from = 1, to = 12, by =1),
                                       labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))))

生成以下图表:

当我们仔细查看图表时,很明显缺少 December,虽然数据中可能缺少这个月,但您仍然希望它出现在图表中。问题的根源在于数据,让我们来看看:

str(data_conrad)
List of 5
 $ Patient: Factor w/ 10 levels " 1"," 2"," 3",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Month  : Factor w/ 11 levels "Apr","Aug","Dec",..: 5 4 6 8 1 3 2 2 4 10 ...
 $ Year   : Factor w/ 3 levels "2010","2011",..: 1 2 3 1 3 2 1 1 3 2 ...
 $ Value  : Factor w/ 11 levels " 1"," 2"," 3",..: 1 2 7 3 4 11 8 8 2 10 ...

我们看到所有变量都是factors,其中应该是ordered factors月份应包括所有十二个月。 应该是integer。让我们把这个正确:

data_conrad2 <- data_conrad
data_conrad2$Month <- factor(data_conrad$Month, 
                             levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
                             ordered = TRUE)
data_conrad2$Month <- factor(data_conrad$Year,
                             levels = ("2010", "2011", "2012"),
                             ordered =  TRUE)
data_conrad2$Value <- as.integer(as.character(data_conrad$Value))

现在我们用新的数据框再次执行代码data_conrad2

lattice::dotplot(Patient ~ Value | Year, 
                 data = data_conrad2, xlab = "Time", ylab = "Patient" , 
                 scales = list(x = list(at = 1:12,
                                       labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))))

添加旋转参数rot可能会提高图形的可读性:

lattice::dotplot(Patient ~ Value | Year, 
                 data = data_conrad2, xlab = "Time", ylab = "Patient" , 
                 scales= list(x = list(at = 1:12,
                                       labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
                                       rot = 45))

)

生成此图:

不过要小心

参数和它们在点图公式中的位置决定了在哪个轴上绘制什么

在上述示例中,患者位于 y 轴上,而价值位于 x 轴上(年份位于不同的方面)。无论您如何重命名标签,无论您如何巧妙地命名 x 轴上的刻度,这就是您所得到的。

为了更清楚一点,下面的代码:

dotplot(Patient  ~ Value  |  Year, 
        data = data_conrad2, xlab = "Value", ylab = "Patient" , 
        scales= list(x = list(at = 1:12))
)

给出这个情节

也许我们习惯看Y轴上的值:

dotplot(Value  ~ Patient  |  Year, 
        data = data_conrad2, xlab = "Patient", ylab = "Value" , 
        scales= list(x = list(at = 1:12))
)

给出这个情节

我希望这对您有所澄清。

【讨论】:

  • 太棒了!非常感谢!这很有帮助!我只需要在这部分添加一个'c':data_conrad2$Month
  • 我的荣幸。很高兴我能帮助你。如果您真的很看重答案,您可以选择用箭头“投票赞成”,或将其标记为“正确答案”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
相关资源
最近更新 更多