I have a dataframe that looks like this:
我有一个如下所示的数据框:
ID Group Measure1 Measure 2
001 A 59 559
002 A 44 623
003 B 129 498
004 C 99 504
005 C 78 378
I want to produce a bar graph that has two sets of labels on the X-axis: one identifying each bar with its ID value, another labeling each bar by Group it belongs to. My data is set up so that members of the same group are adjacent in the dataframe.
我想生成一个条形图,它在X轴上有两组标签:一个用ID值标识每个条形图,另一个用它所属的组标记每个条形图。我的数据已设置,以便同一组的成员在数据框中相邻。
The obvious solution of color-coding the bars by Group is not applicable because I am already using color-coding to show Measure1 and Measure2 (and in some cases Measure3). If there is another way to show the Group information besides labels, I am interested to hear it but I think that two sets of labels at the bottom of my chart is probably the best solution. Here's one such plot with only one set of labels:
按组对条形图进行颜色编码的明显解决方案不适用,因为我已经使用颜色编码来显示Measure1和Measure2(在某些情况下还有Measure3)。如果除了标签之外还有另一种显示组信息的方式,我很想听听,但我认为图表底部的两组标签可能是最好的解决方案。这是一个只有一组标签的情节:
I would like to add the Group labels underneath the "Patient ID" labels.
我想在“患者ID”标签下添加“组”标签。
If I get desperate, I will use Photoshop or Paint to add the labels but I am hoping there is a way to add a second set of labels using R.
如果我绝望,我将使用Photoshop或Paint添加标签,但我希望有一种方法可以使用R添加第二组标签。
1 个解决方案
#1
1
Try:
尝试:
mm = melt(ddf, id=c('ID', 'Group'))
ggplot(mm) + geom_bar(aes(x=interaction(ID, Group), y=value, group=variable, fill=variable), position='dodge', stat='identity')
Separator can be adjusted:
分隔符可以调整:
> with(ddf, interaction(ID, Group, sep="_"))
[1] 1_A 2_A 3_B 4_C 5_C
Levels: 1_A 2_A 3_A 4_A 5_A 1_B 2_B 3_B 4_B 5_B 1_C 2_C 3_C 4_C 5_C
>
> with(ddf, interaction(ID, Group, sep=" "))
[1] 1 A 2 A 3 B 4 C 5 C
Levels: 1 A 2 A 3 A 4 A 5 A 1 B 2 B 3 B 4 B 5 B 1 C 2 C 3 C 4 C 5 C
#1
1
Try:
尝试:
mm = melt(ddf, id=c('ID', 'Group'))
ggplot(mm) + geom_bar(aes(x=interaction(ID, Group), y=value, group=variable, fill=variable), position='dodge', stat='identity')
Separator can be adjusted:
分隔符可以调整:
> with(ddf, interaction(ID, Group, sep="_"))
[1] 1_A 2_A 3_B 4_C 5_C
Levels: 1_A 2_A 3_A 4_A 5_A 1_B 2_B 3_B 4_B 5_B 1_C 2_C 3_C 4_C 5_C
>
> with(ddf, interaction(ID, Group, sep=" "))
[1] 1 A 2 A 3 B 4 C 5 C
Levels: 1 A 2 A 3 A 4 A 5 A 1 B 2 B 3 B 4 B 5 B 1 C 2 C 3 C 4 C 5 C