2017-05-28 22 views
0

請考慮文章末尾的短代碼。 它產生一個條形圖,其中一切都如我所願,除了一些 小調整的傳奇。 我可以控制圖例中 圖例的兩行文本之間的間距,但是如何強制圖例中紅色和 藍色矩形之間的某些間隔? 歡迎任何建議。晶格條形圖:控制圖例中元素的分離

library(lattice) 
library(latticeExtra) 


df_tot<-structure(list(country = structure(c(13L, 1L, 3L, 21L, 12L,  6L, 
22L, 14L, 19L, 20L, 4L, 16L, 9L, 11L, 18L, 17L, 7L, 8L, 2L, 15L, 
10L, 5L, 13L, 1L, 23L, 21L, 12L, 6L, 22L, 14L, 19L, 20L, 4L, 
16L, 9L, 11L, 24L, 18L, 25L, 28L, 26L, 17L, 7L, 8L, 2L, 27L, 
15L, 10L), .Label = c("BE", "PT", "CZ", "EL", "TR", "EE", "NO", 
"PL", "IE", "SI", "IL", "DK", "AT", "FI", "SE", "HU", "NL", "IT", 
"FR", "UK", "DE", "ES", "CY", "IS", "LT", "MT", "RS", "LV"), class = 
"factor"), 
number = c(12L, 1L, 2L, 42L, 11L, 4L, 78L, 12L, 35L, 41L, 
    2L, 21L, 8L, 9L, 25L, 24L, 4L, 4L, 1L, 12L, 8L, 3L, 5L, 1L, 
     1L, 32L, 16L, 3L, 75L, 20L, 16L, 29L, 3L, 9L, 10L, 5L, 1L, 
      33L, 1L, 2L, 1L, 6L, 7L, 2L, 3L, 1L, 11L, 3L), year = 
      c(2015, 
       2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
       2015, 2015, 
        2015, 2015, 2015, 2015, 2015, 2015, 2015, 
        2015, 2015, 2015, 
         2015, 2016, 2016, 2016, 2016, 2016, 2016, 
         2016, 2016, 2016, 
          2016, 2016, 2016, 2016, 2016, 2016, 
          2016, 2016, 2016, 2016, 
           2016, 2016, 2016, 2016, 2016, 
           2016, 2016)), .Names = 
           c("country", 
           "number", "year"), row.names = 
           c(NA, -48L), class = "data.frame") 



p1 <- barchart(number ~ country , 
      groups= as.factor(year), 
         data = df_tot## , 
             , origin=0, spect="fill", 

     par.settings = c(ggplot2like(col=c("blue", "red"))), 
         axis = axis.grid, xlab=list("Number of 
         Beneficiaries", cex=1.2), 
         ylab=list("Country", cex=1.2), 
             main=list(NULL), 
         between = list(x = 1), 
          scales=list(cex=1), auto.key = 
         list(title = "Year", 
         columns=1,space="right",padding.text=3) 
             ) 
             pdf("beneficiaries_all2.pdf", 
         width=15, height=5) 
         print(p1) 
         dev.off() 

回答

1

只需設置height = <something less than 1>,像這樣:

barchart(
    number ~ country , 
    groups = as.factor(year), 
    data = df_tot, 
    origin = 0, 
    aspect = "fill", 
    par.settings = c(ggplot2like(col = c("blue", "red"))), 
    axis = axis.grid, 
    xlab = list("Number of Beneficiaries", cex = 1.2), 
    ylab = list("Country", cex = 1.2), 
    main = list(NULL), 
    between = list(x = 1), 
    scales = list(cex = 1), 
    auto.key = list(
     title = "Year", 
     height = 0.8, 
     columns = 1, 
     space = "right", 
     padding.text = 3 
    ) 
) 

enter image description here

+1

這完全回答我的問題!我在其他地方得到了其他麻煩的答案,但這正是我需要的。 – larry77

+0

@ larry77我很高興它的工作。請考慮接受解決方案作爲答案。 –