小编eal*_*ns1的帖子

如何使用以下R代码使用ggplot2包重现以下图?

用我的数据:

Row | Year | SchoolID | SchoolName | BudgetArea |PaymentPerStudent

001   2011     ABC       PS #1         Staff            12000                
002   2012     ABC       PS #1         Staff            10000
003   2011     ABC       PS #1         Lunch            22000
004   2012     ABC       PS #1         Lunch            18000 
005   2011     DEF       PS #2         Staff            80000
006   2012     DEF       PS #2         Staff            65000
007   2013     DEF       PS #2         Staff            50000
008   2011     DEF       PS #2         Lunch            23000
009   2012     DEF       PS #2         Lunch            34000
010   2013     DEF       PS #2         Lunch …
Run Code Online (Sandbox Code Playgroud)

r graph ggplot2

5
推荐指数
1
解决办法
218
查看次数

如何在ggplot2密度曲线下遮蔽特定区域?

相关文章在这里

我的目的是遮蔽密度曲线下面两个点之间的区域.在这个例子中,我想遮蔽值.25和.5之间的区域.

我已经能够用以下方法绘制我的密度曲线:

    setwd("D:/Workspace")

    # -- create dataframe

    coursename <- c('Math','Math','Math','Math','Math')
    value <- c(.12, .4, .5, .8, .9)
    df <- data.frame(coursename, value)

    library(ggplot2)

    density_plot <- ggplot(aes(x=value, colour=coursename, fill=coursename), data=df) +
                      geom_density(alpha=.3) +
                      geom_vline(aes(xintercept=.5), colour="blue", data=df, linetype="dashed", size=1) +
                      scale_x_continuous(breaks=c(0, .25, .5, .75, 1), labels=c("0", ".25", ".5", ".75", "1")) +
                      coord_cartesian(xlim = c(0.01, 1.01)) +
                      theme(axis.title.y=element_blank(), axis.text.y=element_blank()) +
                      ggtitle("sample data")

    density_plot
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下代码来遮蔽.25和.5之间的区域:

    x1 <- min(which(df$value >=.25))
    x2 <- max(which(df$value <=.5))

    with(density_plot, polygon(x=c(x[c(x1,x1:x2,x2)]), y=c(0, y[x1:x2], 0), col="gray"))
Run Code Online (Sandbox Code Playgroud)

但它只会生成以下错误:

Error in xy.coords(x, y) …
Run Code Online (Sandbox Code Playgroud)

graphics r polygon ggplot2

2
推荐指数
1
解决办法
1805
查看次数

如何使用 ggplot2 在 R 中成功实现“热图绘制和保存”循环

我正在处理调查数据。调查项目有两组,每组三个项目。我的调查样本中有两名受访者。

我正在尝试按调查项目组生成热图,其中:

  • 受访者位于“y”轴上
  • 他们回答的调查项目位于“x”轴上。

这是一个完全可重现的示例:

    wd <- "D:/Desktop/"
    setwd(wd)

    #--create dataframe

    respondent = c("Respondent_1", "Respondent_1", "Respondent_1","Respondent_1", "Respondent_1", "Respondent_1",
                   "Respondent_2", "Respondent_2", "Respondent_2","Respondent_2", "Respondent_2", "Respondent_2")
    item = c("Item_1", "Item_2", "Item_3","Item_1", "Item_2", "Item_3",
             "Item_1", "Item_2", "Item_3","Item_1", "Item_2", "Item_3") 

    item_group = c("Group_1","Group_1","Group_1","Group_2","Group_2","Group_2",
                   "Group_1","Group_1","Group_1","Group_2","Group_2","Group_2")
    score = c(1, 40, 100, 100, 30, 12, 
              2, 15, 80, 77, 44, 10) 

    high_value_color = c("darkred", "darkred", "darkred",
                         "brown3", "brown3", "brown3")

    plot_df = data.frame(respondent, item, item_group, score, high_value_color) 

    #--write function
    #--inspired from this: http://www.reed.edu/data-at-reed/resources/R/loops_with_ggplot2.html

    plot_list <- unique(plot_df$item_group)

    survey_items.graph <- function(df, …
Run Code Online (Sandbox Code Playgroud)

plot for-loop r heatmap ggplot2

1
推荐指数
1
解决办法
1077
查看次数

标签 统计

ggplot2 ×3

r ×3

for-loop ×1

graph ×1

graphics ×1

heatmap ×1

plot ×1

polygon ×1