使用R和ggplot的堆积条形图

时间:2019-03-05 11:58:30

标签: r ggplot2 bar-chart

我在这里尝试了有关堆积条形图的建议,但似乎无法使其正常工作。

我试图绘制数据:

Technology   TodayScenario  WindScenario  BiomassScenario  
Biomass        0.130          0.0646         0.182 
Fossil gas     0.0965         0.00309        0     
Coal           0.218          0              0     
Oil            0.00696        0              0     
PV             0.0328         0.0245         0.0266
Waste          0.0420         0              0     
Onshore        0.323          0.311          0.337 
Offshore       0.150          0.597          0.454

所以我试图用每种方案中每种技术占多少的百分比来绘制每种方案

我已经尝试了以下方法来获得堆积图,但是没有用。

ggplot(Distribution, 
       aes(x = WindScenario, y = Technology) + 
   geom_bar(stat = "identity")

4 个答案:

答案 0 :(得分:1)

为了使数据具有堆叠的条形图,您需要将数据分为三列-技术,值和方案-像这样(您可以将其复制并粘贴到控制台中):

structure(list(Technology = structure(c(1L, 1L, 1L, 3L, 3L, 3L, 
2L, 2L, 2L, 5L, 5L, 5L, 7L, 7L, 7L, 8L, 8L, 8L, 6L, 6L, 6L, 4L, 
4L, 4L), .Label = c("Biomass", "Coal", "Fossil gas", "Offshore", 
"Oil", "Onshore", "PV", "Waste"), class = "factor"), Values = c(0.13, 
0.0646, 0.182, 0.0965, 0.00309, 0, 0.218, 0, 0, 0.00696, 0, 0, 
0.0328, 0.0245, 0.0266, 0.042, 0, 0, 0.323, 0.311, 0.337, 0.15, 
0.597, 0.454), Scenarios = structure(c(2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L), .Label = c("Biomass Scenario", "Today Scenario", "Wind Scenario"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-24L))

按因素对方案数据进行排序:

df$Scenarios <- factor(df$Scenarios,levels = c("Today Scenario",
                                           "Wind Scenario",
                                           "Biomass Scenario"))

安排正确后,您现在可以在ggplot2中输出以下堆叠的条形图:

library(ggplot2)
ggplot(df, aes(fill=Technology, y=Values, x=Scenarios)) + 
   geom_bar( stat="identity")

enter image description here

答案 1 :(得分:0)

切换apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.cn" minSdkVersion 16 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) //noinspection GradleCompatible implementation 'androidx.appcompat:appcompat:1.1.0-alpha02' implementation 'com.google.android.material:material:1.1.0-alpha04' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'de.hdodenhof:circleimageview:3.0.0' implementation 'com.google.firebase:firebase-core:16.0.7' implementation 'com.google.firebase:firebase-auth:16.1.0' implementation 'com.google.firebase:firebase-messaging:17.4.0' apply plugin: 'com.google.gms.google-services' implementation 'com.firebaseui:firebase-ui-database:4.3.1' implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0' implementation 'com.squareup.picasso:picasso:2.71828' implementation 'com.google.firebase:firebase-database:16.1.0' implementation 'com.google.firebase:firebase-storage:16.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.2-alpha01' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2- alpha01' implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha02' } x并合上y的括号:

ggplot()

如果要在垂直轴上显示条形,可以将ggplot(Distribution, aes(x = Technology, y = WindScenario)) + geom_bar(stat = "identity") 添加到绘图中

答案 2 :(得分:0)

ggplot(my_df, 
       aes(x = Technology, y = WindScenario)) + 
  geom_bar(stat = "identity", position = "stack")

答案 3 :(得分:0)

要实现此目的,您需要首先重组数据,使其具有三列:TechnologyScenarioPercentage。像这样:

Technology   Scenario         Percentage     
Biomass      TodayScenario    0.130 
Fossil gas   TodayScenario    0.0965     
Biomass      WindScenario     0.0646
Fossil gas   WindScenario     0.00309
Biomass      BiomassScenario  0.182
Fossil gas   BiomassScenario  0

您可以使用tidyr::gather()

library(tidyr)

my_df %>%
  gather(Scenario, Percentage, c(TodayScenario, WindScenario, BiomassScenario)) %>%
  ggplot(aes(Scenario, Percentage, fill = Technology)) +
  geom_bar(stat = 'identity')