2016-08-25 62 views
1

我想在R中为barplot做两个分类变量,Dep_meds_at_time_of_rx_2phq9_catphq9_cat有两个级别,01,其中0对应于PHQ-L1对应于PHQ-H有条件分组的barplot R

这里是我的代码:

# get counts of vars 
counts <- table(data2$Dep_meds_at_time_of_rx_2, data2$phq9_cat) 

# get percentages of vars 
pcnts <- scale(counts, FALSE, colSums(counts))*100 

# plot barplot 
bp <- barplot(pcnts, beside=TRUE, col=c("azure3", "azure4"), ylab="Frequency (%)", border=NA) 
legend("topright", legend=c("PHQ-L", "PHQ-H"), bty="n", fill=c("azure3", "azure4"), border=NA) 
text(bp, 1, round(pcnts, 2), cex=1, pos=3, col=c("black")) 

,并将所得的情节:

enter image description here

这是伟大的!但我只需要绘制data2$Dep_meds_at_time_of_rx_2==1类别。所以我想要一个只有3.03栏和19.44栏的barplot。

我已经用尽任何聪明的技巧,我已经知道了这样的作为使data2$Dep_meds_at_time_of_rx_2==0条白色和使用space = c(-1, 0)使data2$Dep_meds_at_time_of_rx_2==1条彼此相邻但随后的酒吧超宽,就像这样:

enter image description here

我只需要data2$Dep_meds_at_time_of_rx_2==1列,但宽度正常。

任何想法?

这里是我的数据:

> dput(data2) 
structure(list(Dep_meds_at_time_of_rx_2 = c(0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,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 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L)), .Names = c("Dep_meds_at_time_of_rx_2", "phq9_cat"), row.names = c(NA, 
-243L), class = "data.frame") 

回答

2

这里是你有什么是次要版本。我想你想什么它得到接近完成:

bp <- barplot(pcnts[2,], beside=TRUE, col=c("azure3", "azure4"), ylab="Frequency (%)", 
       border=NA) 
legend("topleft", legend=c("PHQ-L", "PHQ-H"), bty="n", fill=c("azure3", "azure4"), border=NA) 
text(bp[], 1, round(pcnts[2,], 2), cex=1, pos=3, col=c("black")) 

注意,如果希望放弃在x轴上的“0”和“1”的标签,你可以通过与unname(pcnts)更换pcnts[2, ]做到这一点第一行:

bp <- barplot(unname(pcnts[2, ]), beside=TRUE, col=c("azure3", "azure4"), 
       ylab="Frequency (%)", border=NA) 

enter image description here

+0

不要忘了选择pcnts'的'第二行text()命令,否则你会得到重叠的标签。 – jkeirstead

+0

赞,谢谢。我有,但必须复制早期版本。 – lmo

+0

谢谢!这很棒。不知道为什么我不记得如何选择表格的第二行。我会假装它是星期一。 – nchimato

1

你只需要选择您的百分比表,例如第二行

# get percentages of vars 
pcnts <- scale(counts, FALSE, colSums(counts))*100 
# Filter for the results you want 
pcnts <- pcnts[2, ] 
# Plot as before 
+0

这正是我所需要的。提高了一个,但有人很快就得到了答案。谢谢! – nchimato

1

如果你想实现窄条,那么宽度和空间参数的组合就可以实现。

barplot(pcnts, beside=TRUE, col=c("azure3", "azure4"), ylab="Frequency (%)", border=NA, width = c(0,.51, 0,0.51), space = c(1,2)) 

enter image description here

,或者你可以我想我会在ggplot2答案扔颜色更改为白色

barplot(pcnts, beside=TRUE, col=c("white", "azure4"), ylab="Frequency (%)", border=NA, space = c(1,2)) 

enter image description here

+0

Up投票使用'width'和'space'参数,我之前没有看过那个组合。这些标签只是不与酒吧居中。看起来像选择'pcnts'的第二排是我正在寻找的。谢谢! – nchimato

2

。该解决方案确保了在x轴的标签是1 - 反映的抗抑郁使用状态:

library(ggplot2) 

df1 <- data.frame(Frequency = pcnts[2,], 
        PHQ = c('PHQ-L','PHQ-H')) 

ggplot(df1, aes(x = 1, y = Frequency))+ 
    geom_bar(stat = 'identity', aes(fill = PHQ), 
      position = position_dodge(width = 1))+ 
    scale_fill_manual(values = c('PHQ-L' = 'azure3', 
           'PHQ-H' = 'azure4'), 
         name = '')+ 
    scale_x_continuous(breaks = c(.75, 1.25), 
         labels = c(1,1))+ 
    xlab('Anti-Depressant use at time of treatment')+ 
    ylab('Frequency (%)')+ 
    geom_text(x = .75, y = 2.5, label = '19.44%')+ 
    geom_text(x = 1.25, y = 2.5, label = '3.03%')+ 
    theme_bw() 

enter image description here

+0

这真的很有用,因为我也尝试在'ggplot'中弄清楚它。我将不得不在下次尝试。谢谢你的信息! – nchimato