如何在条形图功能(格子R包)中使用ylim设置短轴刻度

时间:2014-03-27 22:04:04

标签: r lattice

是否有人对使用ylim函数中的barchart选项有任何见解?我试图做的应该很简单,但我在设置次要刻度标记时遇到问题。我正在绘制一个范围从1:9的连续整数,并且我希望每个值(1,2,3,4,5,6,7,8,9)都显示在图表上。

以下是一些示例代码......

ratings <- c(4, 4.1, 4, 4.6, 3.8, 4.3)
group <- c("X", "Y", "C", "Y", "X", "Y")
cond <- c("C", "C", "L", "L", "S", "S")

meandata <- data.frame(ratings, group, cond)

attach(meandata)

group <- as.factor(group)
cond <- as.factor(cond)


bar=barchart(ratings~cond, groups = group, 
auto.key = TRUE, par.settings = my.settings,
xlab = "Condition",
ylab = "Attraction Ratings",
main = "Attraction Ratings as a function of Condition and Group",
ylim = c(1:9)
yscale.components=
)

bar

2 个答案:

答案 0 :(得分:3)

您可以在scales中使用barchart参数。

library(lattice)
barchart(ratings ~ cond, groups = group, ylim = 1:9,
         auto.key = TRUE, xlab = "Condition", 
         ylab = "Attraction Ratings",
         main = "Attraction Ratings as a function of Condition and Group",
         scales = list(y = list(at = c(1:9))))

enter image description here

答案 1 :(得分:1)

在第二次看后,我想我可能知道你的意思是使用ylim。

group <- as.factor(group)
cond <- as.factor(cond)
bar=barchart(ratings~cond, groups = group, 
auto.key = TRUE,
xlab = "Condition",
ylab = "Attraction Ratings",
main = "Attraction Ratings as a function of Condition and Group",
ylim = c(1:9), 
scales=list(y=list(at=1:9, labels=1:9))
)
png("out.png")
print(bar); dev.off()

enter image description here

顺便说一句:我使用非零值的条形图通常是一个坏主意。这是“撒谎与统计学”的常见方式,因为它夸大了差异。

我之前的猜测是使用了latticeExtra函数yscale.components.subticks

library(latticeExtra)
bar=barchart(ratings~cond, groups = group, 
auto.key = TRUE,
xlab = "Condition",
ylab = "Attraction Ratings",
main = "Attraction Ratings as a function of Condition and Group",
ylim = c(1:9), tck=c(5,1),
yscale.components=yscale.components.subticks
)

bar

enter image description here