r抖动堆积条形图

时间:2019-04-21 21:30:34

标签: r ggplot2 label jitter

我正在绘制一个堆叠的箱形图以表示下面的数据

  Choice   Race   Freq countT per     
   <fct>    <ord> <dbl>  <dbl> <chr>   
 1 Pepsi    Asian   362  20684 1.7501% 
 2 Coke     Asian   363  20684 1.755%  
 3 Sprite   Asian   204  20684 0.9863% 
 4 Gatorade Asian     1  20684 0.0048% 
 5 Pepsi    Black   150  20684 0.7252% 
 6 Coke     Black   408  20684 1.9725% 
 7 Sprite   Black   322  20684 1.5568% 
 8 Gatorade Black    45  20684 0.2176% 
 9 Pepsi    Other  1088  20684 5.2601% 
10 Coke     Other  2407  20684 11.637% 
11 Sprite   Other   223  20684 1.0781% 
12 Gatorade Other   113  20684 0.5463% 
13 Pepsi    White  2384  20684 11.5258%
14 Coke     White  5038  20684 24.357% 
15 Sprite   White  6882  20684 33.2721%
16 Gatorade White   694  20684 3.3553% 

这是我的下面的代码

library(ggplot2)

colortest <- c("#FBCF357F", "#ED4C1C7F", "#9C7E707F", "#5AC2F17F" )

ggplot(testdf, aes(x = Choice, y = Freq, fill = Race, label = Freq)) +
  geom_bar(stat = "identity") + 
  geom_text(size = 4, position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values =  colortest) +
  scale_colour_manual(values = colortest) +
  coord_flip() +
  theme_bw() +
  ylab("Count by Race")

我看到的是一系列重叠的标签,这些标签通常很难阅读。

enter image description here

如何偏移这些重叠标签的位置,以便看到没有重叠的数字?

enter image description here

我尝试了许多其他抖动选项,但所有这些都失败了。

1 个答案:

答案 0 :(得分:0)

使用import UIKit import WebKit class WebViewController: UIViewController, WKNavigationDelegate { @IBOutlet weak var webView: WKWebView! override func viewDidLoad() { super.viewDidLoad() config() } private func config(){ webView.navigationDelegate = self guard let url = URL(string: "url") else { return } webView.load(URLRequest(url: url)) } func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { let css = "Css Code" let js = "var style = document.createElement('style'); style.innerHTML = '\(css)'; document.head.appendChild(style);" webView.evaluateJavaScript(js, completionHandler: nil) } } 轻松而俗气。 :-)

enter image description here

代码

ggrepel

数据

library(ggplot2)
library(ggrepel)

colortest <- c("#FBCF357F", "#ED4C1C7F", "#9C7E707F", "#5AC2F17F" )

ggplot(df, aes(x = Choice, y = Freq, fill = Race, label = Freq)) +
  geom_bar(stat = "identity") + 
  # Here it is
  geom_text_repel(size = 4, position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values =  colortest) +
  scale_colour_manual(values = colortest) +
  coord_flip() +
  theme_bw() +
  ylab("Count by Race")
相关问题