【问题标题】:ggplot2 dotplot with facet_grid with labels on top ala facet_wrap (but w/ space = "free_x")?ggplot2 dotplot with facet_grid 顶部有标签 ala facet_wrap (但带有空格=“free_x”)?
【发布时间】:2012-05-23 18:35:52
【问题描述】:

我真的很喜欢我的 dotplot 与 facet_wrap(顶部的构面标签)的外观,但我希望能够传递一个空格 =“free_x”,以便适当地调整构面的大小。 facet_grid 的问题是构面标签移动到绘图的一侧,在这种情况下效果不佳,因为我希望每个面板都分开。

代码如下:

# load data
plotdat <- structure(list(level = c("Lost (N =328)", "Won (N =75)", "Lost (N =10)", 
                        "Won (N =65)", "Challenger (N =318)", "Incumbent (N =85)", "Arab (N =7)", 
                        "Black (N =222)", "East Asian (N =40)", "Latino (N =107)", "Other (N =10)", 
                        "South Asian (N =17)", "Not (N =252)", "Statewide (N =151)"), 
                mean = c(0.59834264517378, 0.645308353066667, 0.6382179387, 
                        0.646399186046154, 0.595756747751572, 0.649457274258823, 
                        0.682776774142857, 0.557334915725225, 0.6654738063, 0.68260777364486, 
                        0.6061308922, 0.613378378411765, 0.616298597519841, 0.591703758423841
                ), se = c(0.00597842210656315, 0.0113080614816089, 0.044927778673023, 
                        0.011274258338002, 0.00622316181664198, 0.00900474213888581, 
                        0.0247451786416615, 0.00690804451732034, 0.0116899960061005, 
                        0.00777478853477299, 0.0183766282892234, 0.0166464474073244, 
                        0.00669527297092827, 0.00887170639612841), N = c(328L, 75L, 
                        10L, 65L, 318L, 85L, 7L, 222L, 40L, 107L, 10L, 17L, 252L, 
                        151L), var = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 
                                4L, 4L, 4L, 4L, 5L, 5L), .Label = c("Primary Election", "General Election", 
                                "Incumbency", "Race", "Statewide District"), class = "factor")), .Names = c("level", 
                "mean", "se", "N", "var"), row.names = c(NA, 14L), class = "data.frame")
library('ggplot2')

# with facet_wrap:
ggplot(plotdat, aes(x = mean, xmin = mean-se, xmax = mean+se, y = level)) + 
    geom_point() + geom_segment( aes(x = mean-se, xend = mean+se, 
                    y = level, yend=level)) +
    facet_wrap(~var, ncol=1, scales = "free_y") +
    theme_bw() + opts(axis.title.x = theme_text(size = 12, vjust = .25))+ 
    xlab("Mean V (Brightness) for Candidate's Face") + ylab("") +
    opts(title = expression("Skin Complexion for 2010 Minority Candidates"))

# with facet_grid:
ggplot(plotdat, aes(x = mean, xmin = mean-se, xmax = mean+se, y = level)) + 
    geom_point() + geom_segment( aes(x = mean-se, xend = mean+se, 
                    y = level, yend=level)) +
    facet_grid(var~., scales = "free_y", space = "free_y") +
    theme_bw() + opts(axis.title.x = theme_text(size = 12, vjust = .25))+ 
    xlab("Mean V (Brightness) for Candidate's Face") + ylab("") +
    opts(title = expression("Skin Complexion for 2010 Minority Candidates"))

有什么建议吗?非常感谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    更新 使用 ggplot grob,这很容易做到。见herehere

    ggplot grob 版本

    library(ggplot2)
    library(dplyr)
    library(grid)
    
    # Get the plot; plotdat data frame is below
    p = ggplot(plotdat, aes(x = mean, xmin = mean-se, xmax = mean+se, y = level)) + 
        geom_point() + geom_segment( aes(x = mean-se, xend = mean+se, 
                        y = level, yend=level)) +
        facet_wrap(~var, ncol=1, scales = "free_y") +
        theme_bw() + theme(axis.title.x = element_text(size = 12, vjust = .25))+ 
        xlab("Mean V (Brightness) for Candidate's Face") + ylab("") +
        ggtitle("Skin Complexion for 2010 Minority Candidates")
    
    
    # From 'plotdat', get the number of 'levels' for each 'var'.
    # That is, the number y-breaks in each panel.
    N <- plotdat %>% group_by(var) %>% 
       summarise(count = n()) %>% 
       `[[`(2)
    
    # Get the ggplot grob
    gt = ggplotGrob(p) 
    
    # Get the locations of the panels in the gtable layout.
    panels <- gt$layout$t[grepl("panel", gt$layout$name)]
    
    # Replace the default panel heights with relative heights
    gt$heights[panels] <- unit(N, "null")
    
    ## Draw gt
    grid.newpage()
    grid.draw(gt)
    




    原答案 编辑:更新到ggplot2 0.9.3.1 版

    这不会回答你的问题。它调整了facet_grid 的外观。

    我不确定您所说的“每个面板都被分开”是什么意思。如果您担心facet_grid 图中的条形文本超出条形边界,可以使用theme(strip.text.y = element_text(angle = 0)) 旋转文本。此外,可以使用 stingr 包中的 str_wrap 将文本环绕成多行。

    # load data
    plotdat <- structure(list(level = c("Lost (N =328)", "Won (N =75)", "Lost (N =10)", 
                            "Won (N =65)", "Challenger (N =318)", "Incumbent (N =85)", "Arab (N =7)", 
                            "Black (N =222)", "East Asian (N =40)", "Latino (N =107)", "Other (N =10)", 
                            "South Asian (N =17)", "Not (N =252)", "Statewide (N =151)"), 
                    mean = c(0.59834264517378, 0.645308353066667, 0.6382179387, 
                            0.646399186046154, 0.595756747751572, 0.649457274258823, 
                            0.682776774142857, 0.557334915725225, 0.6654738063, 0.68260777364486, 
                            0.6061308922, 0.613378378411765, 0.616298597519841, 0.591703758423841
                    ), se = c(0.00597842210656315, 0.0113080614816089, 0.044927778673023, 
                            0.011274258338002, 0.00622316181664198, 0.00900474213888581, 
                            0.0247451786416615, 0.00690804451732034, 0.0116899960061005, 
                            0.00777478853477299, 0.0183766282892234, 0.0166464474073244, 
                            0.00669527297092827, 0.00887170639612841), N = c(328L, 75L, 
                            10L, 65L, 318L, 85L, 7L, 222L, 40L, 107L, 10L, 17L, 252L, 
                            151L), var = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 
                                    4L, 4L, 4L, 4L, 5L, 5L), .Label = c("Primary Election", "General Election", 
                                    "Incumbency", "Race", "Statewide District"), class = "factor")), .Names = c("level", 
                    "mean", "se", "N", "var"), row.names = c(NA, 14L), class = "data.frame")
    library('ggplot2')
    library(stringr)
    
    plotdat$var = str_wrap(plotdat$var, width = 10)
    
    # with facet_grid:
    ggplot(plotdat, aes(x = mean, xmin = mean-se, xmax = mean+se, y = level)) + 
        geom_point() + geom_segment( aes(x = mean-se, xend = mean+se, 
                        y = level, yend=level)) +
        facet_grid(var~., scales = "free_y", space = "free_y") +
        theme_bw() + 
        ggtitle("Skin Complexion for 2010 Minority Candidates") +
        xlab("Mean V (Brightness) for Candidate's Face") + ylab("") +
        theme(axis.title.x = element_text(size = 12, vjust = .25), 
              strip.text.y = element_text(angle = 0))
    

    如果“要分隔的面板”表示“面板之间的额外空间”,请在加载 grid 后使用 theme(panel.margin = unit(2, "line")

    library(grid)
    ggplot(plotdat, aes(x = mean, xmin = mean-se, xmax = mean+se, y = level)) + 
        geom_point() + geom_segment( aes(x = mean-se, xend = mean+se, 
                        y = level, yend=level)) +
        facet_grid(var~., scales = "free_y", space = "free_y") +
        theme_bw() + 
        ggtitle("Skin Complexion for 2010 Minority Candidates") +
        xlab("Mean V (Brightness) for Candidate's Face") + ylab("") +
        theme(axis.title.x = element_text(size = 12, vjust = .25), 
              strip.text.y = element_text(angle = 0),
              panel.margin = unit(2, "lines"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2020-10-13
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      相关资源
      最近更新 更多