How to set minor axis ticks using ylim in the barchart function (lattice R package)

StackOverflow https://stackoverflow.com/questions/22700188

  •  22-06-2023
  •  | 
  •  

문제

Would anyone have any insight on using the ylim option in the barchart function? What I am attempting to do should be simple but I am having trouble with setting the minor tick marks. I am plotting a continuous integer that ranges from 1:9 and I'd like for each value (1,2,3,4,5,6,7,8,9) to be displayed on the graph.

Here is some example code...

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
도움이 되었습니까?

해결책

You can use the scales argument in barchart for this.

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

다른 팁

After taking a second look I think I might know you meant about using 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

BTW: I it's generally a bad idea to use barcharts with non-zero values for the lower limit. It's a common way of "lying with statsitics" since it exaggerates differences.

I left my earlier guess in using the latticeExtra function 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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top