Сдвиг точек по оси х - PullRequest
0 голосов
/ 03 июня 2018

Я пытаюсь воссоздать пополам сюжет с рамкой и точками.В идеале я бы хотел, чтобы точки располагались слева от прямоугольника, а не перекрывались.до сих пор я следовал этому -

https://github.com/h21k/R/blob/master/snippets/half_box.R

Однако я удалил функцию as.numeric в строке 29.

возможно ли сместить мои точки вслева в его случае, чтобы они не перекрывались с рамкой, или, что еще лучше расположить их в горизонтальной гистограмме рядом с каждой рамкой?

Надеюсь, это понятно.

Соответствующий код из скрипта:

library(ggplot2);library(ggthemes)

ggplot(iris) +
  theme_stata() +
  theme(line = element_blank()) + 
  stat_boxplot(aes(x = Species, y = Sepal.Length), geom='errorbar', linetype=1, width=0.2) + 
  geom_boxplot(aes(x = Species, y = Sepal.Length, fill=Species),
               alpha = 1, size = 0.75, width = 0.25, outlier.shape = 3) + 
  annotate("rect", xmin = 1, xmax = 1.5, ymin = 0, ymax = 8, alpha = 1, fill = 'white') +
  annotate("rect", xmin = 2, xmax = 2.5, ymin = 0, ymax = 8, alpha = 1, fill = 'white') +
  annotate("rect", xmin = 3, xmax = 3.5, ymin = 0, ymax = 8, alpha = 1, fill = 'white') +
  geom_point(aes(x = as.numeric(Species) + 0.1, colour = Species, y = Sepal.Length),
             alpha = 0.5, position = position_jitter(width = 0.1))

1 Ответ

0 голосов
/ 04 июня 2018

Я не совсем уверен, если вы этого хотите, но вы можете сделать это, чтобы расположить их друг над другом:

require(tidyverse)

boxplot <- ggplot(iris) +
  theme_stata() +
  theme(line = element_blank(), legend.position="none") + 
  stat_boxplot(aes(x = Species, y = Sepal.Length), geom='errorbar', linetype=1, width=0.2) + 
  geom_boxplot(aes(x = Species, y = Sepal.Length, fill=Species),
               alpha = 1, size = 0.75, width = 0.25, outlier.shape = 3)

histogram <- ggplot(iris) +
  theme_stata() +
  theme(line = element_blank()) + 
  geom_histogram(aes(Sepal.Length, fill=Species),
               alpha = 1, size = 0.75) +
  facet_grid(~Species)


ggpubr::ggarrange(boxplot, histogram, nrow = 2)
...