如何在格子点图中添加月份和年份而不是数字?

时间:2019-06-03 21:57:28

标签: r lattice

我对R还是比较陌生,每当我尝试向点图添加月份和年份时都会遇到问题。当我使用晶格运行数据时,我会像这样:

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))))

This is how my plot looks like after running the code

但是,我尝试获取实际月份而不是(1,2,3,4 ... 12),并且尝试将"Data_Conrad$Year"替换为实际年份(2010、2011 (2012年),但是我对此有疑问。

键入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))

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

使用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"))))

产生下图:

enter image description here

当我们仔细查看图表时,很明显 12月丢失了,尽管数据中可能缺少本月,但您仍然希望它出现在图表中。问题的根源在于数据,让我们看一下:

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"))))

enter image description here

添加旋转参数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))

产生此图:

enter image description here

但是要小心

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

在上述示例中,您的通话中,Patient位于y轴,Value位于x轴(年份在不同的方面)。无论您如何重命名标签,无论您如何巧妙地在x轴上命名刻度,这都是您所得到的。

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

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

给出这个情节

enter image description here

也许我们习惯于在Y轴上看到该值:

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

给出这个情节

enter image description here

我希望这对您有所帮助。