ggplot和geom_text()

时间:2015-11-27 17:18:29

标签: r ggplot2 bar-chart

我是R和CREATE TABLE posts ( title CHAR(20), content CHAR(42), x_coor INTEGER, y_coor INTEGER, userID CHAR(50), time_stamp TIMESTAMP default current_timestamp, postID SERIAL, PRIMARY KEY(postID), FOREIGN KEY (userID) REFERENCES users ON DELETE CASCADE); 的新手。我已经对此进行了大量搜索,但我找不到解决方案。

ggplot2

我打算用Sample observation1 observation2 observation3 percentage sample1_A 163453473 131232689 61984186 30.6236955883 Sample1_B 170151351 137202212 59242536 26.8866816109 sample2_A 194102849 162112484 89158170 40.4183031852 sample2_B 170642240 141888123 79925652 41.7493687378 sample3_A 192858504 161227348 90532447 41.8068248626 sample3_B 177174787 147412720 81523935 40.5463120438 sample4_A 199232380 174656081 118115358 55.6409038531 sample4_B 211128931 186848929 123552556 54.7201927527 sample5_A 186039420 152618196 87012356 40.9656544833 sample5_B 145855252 118225865 66265976 39.5744515254 sample6_A 211165202 186625116 112710053 48.5457722338 sample6_B 220522502 193191927 114882014 47.238670909 绘制条形图。我想将前三列绘制为条形图“闪避”,并用百分比标记ggplot2条。我可以如下绘制条形图,但我无法使用observation3添加标签。

geom_text()

2 个答案:

答案 0 :(得分:1)

data1转换为长格式,将观察列作为度量变量,将Samplepercentage列作为id变量。计算用于放置百分比的最大值mx。然后执行绘图。请注意,geom_bar使用data1.longgeom_text使用data1我们为文本着色,使百分比与observation3条颜色相同。(有关如何指定默认颜色,请参阅this post。)两者都继承aes(x = Sample)但使用不同的y和其他美学。我们通过从data1$Sample(可选)中删除所有小写字母和下划线来清理X轴标签。

library(ggplot2)
library(reshape2)

data1.long <- melt(data1, measure = 2:4)  # cols 2:4 are observation1, ..., observation3
mx <- max(data1.long$value)  # maximum observation value
ggplot(data1.long, aes(x = Sample, y = value)) +
   geom_bar(aes(fill = variable), stat = "identity", width = 0.5, position = "dodge") + 
   geom_text(aes(y = mx, label = paste0(round(percentage), "%")), data = data1, 
        col = "#619CFF", vjust = -0.5) +
   scale_x_discrete(labels = gsub("[a-z_]", "", data1$Sample))

(点击图表放大)

screenshot

注意:我们使用了这些数据。请注意,一次出现的Sample更改为带有小写s的样本:

Lines <- "Sample  observation1    observation2    observation3    percentage
sample1_A   163453473   131232689   61984186    30.6236955883
sample1_B   170151351   137202212   59242536    26.8866816109
sample2_A   194102849   162112484   89158170    40.4183031852
sample2_B   170642240   141888123   79925652    41.7493687378
sample3_A   192858504   161227348   90532447    41.8068248626
sample3_B   177174787   147412720   81523935    40.5463120438
sample4_A   199232380   174656081   118115358   55.6409038531
sample4_B   211128931   186848929   123552556   54.7201927527
sample5_A   186039420   152618196   87012356    40.9656544833
sample5_B   145855252   118225865   66265976    39.5744515254
sample6_A   211165202   186625116   112710053   48.5457722338
sample6_B   220522502   193191927   114882014   47.238670909"

data1 <- read.table(text = Lines, header = TRUE)

更新次要改进

答案 1 :(得分:0)

可能G. Grothendieck's answer是更好的解决方案,但这是我的建议(下面的代码)

enter image description here

 <form class="well" action="upload.php" method="post" enctype="multipart/form-data">
              <div class="form-group">
                <label for="file">Select a file to upload</label>
                <input type="file" name="file">
                <p class="help-block">Only jpg file with maximum size of 1 MB is allowed.</p>
              </div>
              <input type="submit" class="btn btn-lg btn-primary" value="Upload">
            </form>