문제

I am trying to create a combined bar and xyplot using lattice where the x-axis is shared by both plots.

require(lattice)
require(latticeExtra)
dd <- data.frame(Year = factor(1990:1999), Count = 0:9, Size = 9:0)
p1 <- barchart(Count ~ Year, data = dd, horizontal = FALSE)
p2 <- xyplot(Size ~ Year, data = dd)
c(p1,p2, x.same = TRUE, layout = c(1,2))

enter image description here

This plot is not satisfactory because latticeExtra adds extra gaps for 1 and 10 to the x-axis when combining plots. These gaps do not appear on the individual constituent plots.

How can I remove 1 and 10 from the x-axis?

As pointed out to me my problem in this special case can be solved by deleting the x.same argument. However this is not a general solution and only works because my example was unsatisfactory. An improved example is shown below.

dd1 <- data.frame(Year = factor(1990:1999), Count = 0:9)
p1 <- barchart(Count ~ Year, data = dd1, horizontal = FALSE)


dd2 <- data.frame(Year = factor(1991:2000), Size = 0:9)
p2 <- xyplot(Size ~ Year, data = dd2, type = "o")
p2

Removing x.same yields:

c(p1,p2, layout = c(1,2))

enter image description here

This plot is not satisfactory as it has two x-axes.

(I asked an earlier but different question in an attempt to fix this problem).

도움이 되었습니까?

해결책

Maybe you need to start with the same factor levels for both plots.

# Using factors

dd1 <- data.frame(Year = factor(1990:2000), Count = c(0:9,NA))
p1 <- barchart(Count ~ Year, data = dd1, horizontal = FALSE)


dd2 <- data.frame(Year = factor(1990:2000), Size = c(NA,0:9))
p2 <- xyplot(Size ~ Year, data = dd2, type = "o")
p2

c(p1,p2, layout = c(1,2)) # works
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top