ggplot中的分组条形图,y值基于2个分类变量的组合?

时间:2017-01-17 02:25:10

标签: r plot ggplot2 bar-chart

我正在尝试在ggplot中创建一个分组条形图,其中每个x值应该有4个条形图。这是我的数据的一个子集(实际数据大约长4倍):

Verb_Type,Frame,proportion_type,speaker
mental,V CP,0.209513024,Child
mental,V NP,0.138731597,Child
perception,V CP,0.017167382,Child
perception,V NP,0.387528402,Child
mental,V CP,0.437998087,Parent
mental,V NP,0.144086707,Parent
perception,V CP,0.042695836,Parent
perception,V NP,0.398376853,Parent

我想要的是将Frame绘制为x值,将proportion_type绘制为y值,但使用基于Verb_Type和speaker的条形图。因此,对于每个x值(Frame),将有4个条组合在一起 - 每个对应于mental_child,mental~parent,perception_child,perception~parent的proportion_type值的条形图。我需要填充颜色基于Verb_Type,并填充基于扬声器的“纹理”(饱和度等)。我想要堆叠条,因为它不能准确地表示数据。

我不想使用facet网格,因为我发现在将它们分成2组时,很难比较所有4个条形图。我想将所有条形图组合在一起,以便更容易实现可视化。但我无法弄清楚如何进行适当的分组。这是我可以在ggplot中做的事情,还是我需要在绘图之前操纵数据?我尝试使用熔化来重塑数据,但要么我做错了,要么就是我不应该做的事情。

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { LocationStrategy, HashLocationStrategy } from '@angular/common'; import { HttpModule, JsonpModule, Http, RequestOptions, XHRBackend, RequestOptionsArgs, Response, ConnectionBackend} from '@angular/http'; import { AppRoutingModule } from './app.routes'; import { AppComponent } from './app.component'; import { CategoriesComponent } from './components/admin/categories/categories.component'; import { LoadingInterceptor } from './assets/scripts/services/loadingInterceptor'; import { EventsEmitter } from './assets/scripts/services/eventsEmitter'; import { ToasterModule} from 'angular2-toaster'; @NgModule({ imports: [AppRoutingModule, BrowserModule, FormsModule, ReactiveFormsModule, HttpModule, JsonpModule, ToasterModule ], declarations: [AppComponent, CategoriesComponent], bootstrap: [AppComponent], providers: [EventsEmitter,LoadingInterceptor, { provide: Http, useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions, eventsEmitter: EventsEmitter) => new LoadingInterceptor(xhrBackend, requestOptions, eventsEmitter), deps: [XHRBackend, RequestOptions,EventsEmitter] },{ provide: LocationStrategy, useClass: HashLocationStrategy }] }) export class AppModule { } interaction()之间的df$Verb_Type(即获得所有唯一配对)以获取您所追求的列分组。您可以将其直接传递给df$speaker或提前创建一个新变量:

ggplot

或者:

ggplot(df, aes(x = Frame, y = proportion_type,
               group = interaction(Verb_Type, speaker), fill = Verb_Type, alpha = speaker)) +
    geom_bar(stat = "identity", position = "dodge") +
    scale_alpha_manual(values = c(.5, 1))

enter image description here