Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 箱线图和点图能否来自ggplot中的两个不同数据?_R_Ggplot2_Boxplot - Fatal编程技术网

R 箱线图和点图能否来自ggplot中的两个不同数据?

R 箱线图和点图能否来自ggplot中的两个不同数据?,r,ggplot2,boxplot,R,Ggplot2,Boxplot,我希望我的方框图包含单个数据点,但我希望从单独的数据集中提取这些点 例如,如果我的数据帧(“df”)如下所示: ID <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) Happiness <- c(2, 3, 10, 7, 6, 8, 3, 9, 5, 1) Smoke <- c("yes", "yes", "no", "yes", "no", "

我希望我的方框图包含单个数据点,但我希望从单独的数据集中提取这些点

例如,如果我的数据帧(“df”)如下所示:

ID <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Happiness <- c(2, 3, 10, 7, 6, 8, 3, 9, 5, 1)
Smoke <- c("yes", "yes", "no", "yes", "no", "no", "no", "no", "yes", "no")
Exercise <- c("no", "yes", "no", "yes", "yes", "yes", "yes", "no", "no", "yes")

通常,当我看到点覆盖在箱线图上时,我假设它们代表相同的东西(即箱线图显示分布,点显示每个单独的值)。如果你对吸烟和锻炼之间的相互作用感兴趣,那么画一个图也许更有意义

library(tidyverse)
ID <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Happiness <- c(2, 3, 10, 7, 6, 8, 3, 9, 5, 1)
Smoke <- c("yes", "yes", "no", "yes", "no", "no", "no", "no", "yes", "no")
Exercise <- c("no", "yes", "no", "yes", "yes", "yes", "yes", "no", "no", "yes")
df <- tibble("ID" = ID, "Happiness" = Happiness,
                 "Smoke" = Smoke, "Exercise" = Exercise)
df %>% 
  mutate(Smoke = ifelse(Smoke == "yes",
                        "Smoker",
                        "Non-Smoker"),
         Exercise = ifelse(Exercise == "yes",
                           "Exercises",
                           "Doesn't Exercise"),
         Interaction = factor(str_replace(interaction(Smoke, Exercise),
                                          '\\.', '\n'),
                              ordered=TRUE)) %>% 
  ggplot(aes(x= Interaction, y = Happiness)) + 
  geom_boxplot(aes(fill = Smoke)) +
  geom_point(aes(shape = Exercise), size = 4) +
  labs(title = "Happiness by Smoking/Exercise",
       y = "Happiness") +
  theme_classic(base_size = 16) +
  theme(axis.title.x = element_blank())
库(tidyverse)
ID可以使用geom_point()代替geom_dotplot()


df这是真的。。。老实说,当我问这个问题时,我脑子里也不太清楚我在想什么样的阴谋。这个互动情节很有意义。非常感谢你!嗨,我在想--有没有可能用雨云图创建一个交互图?()是的,我在某个地方有一个脚本,但是在这个例子中它没有真正的意义,因为你只为每个变量绘制了少量的点(例如,2点-不能绘制分布图)。我将编辑我的答案,包括一个更新的最小可复制示例和一种绘制雨云的方法。这非常完美,非常感谢!!没问题,也不需要说谢谢——提前付款,回答其他人的问题。谢谢你投票/接受我的答案:)我认为这看起来更接近我的初衷——但我也同意你和上面的评论,这是令人困惑的,可能会误导。无论如何,非常感谢你的帮助!
library(tidyverse)
ID <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Happiness <- c(2, 3, 10, 7, 6, 8, 3, 9, 5, 1)
Smoke <- c("yes", "yes", "no", "yes", "no", "no", "no", "no", "yes", "no")
Exercise <- c("no", "yes", "no", "yes", "yes", "yes", "yes", "no", "no", "yes")
df <- tibble("ID" = ID, "Happiness" = Happiness,
                 "Smoke" = Smoke, "Exercise" = Exercise)
df %>% 
  mutate(Smoke = ifelse(Smoke == "yes",
                        "Smoker",
                        "Non-Smoker"),
         Exercise = ifelse(Exercise == "yes",
                           "Exercises",
                           "Doesn't Exercise"),
         Interaction = factor(str_replace(interaction(Smoke, Exercise),
                                          '\\.', '\n'),
                              ordered=TRUE)) %>% 
  ggplot(aes(x= Interaction, y = Happiness)) + 
  geom_boxplot(aes(fill = Smoke)) +
  geom_point(aes(shape = Exercise), size = 4) +
  labs(title = "Happiness by Smoking/Exercise",
       y = "Happiness") +
  theme_classic(base_size = 16) +
  theme(axis.title.x = element_blank())
# Load libraries
library(tidyverse)

# Get data
ID <- seq(1:50)
Happiness <- sample(1:100, 50, replace = TRUE)
Smoke <- sample(c("yes", "no"), 50, replace = TRUE)
Exercise <- sample(c("yes", "no"), 50, replace = TRUE)
df <- tibble("ID" = ID, "Happiness" = Happiness,
             "Smoke" = Smoke, "Exercise" = Exercise)

# Source Ben Marwick's code for Violin Plots
source("https://gist.githubusercontent.com/benmarwick/2a1bb0133ff568cbe28d/raw/fb53bd97121f7f9ce947837ef1a4c65a73bffb3f/geom_flat_violin.R")

# Raincloud plot theme
raincloud_theme = theme(
  text = element_text(size = 14),
  axis.title.x = element_text(size = 14),
  axis.title.y = element_blank(),
  axis.text = element_text(size = 14),
  axis.text.y = element_text(vjust = 0.3),
  legend.title=element_text(size=14),
  legend.text=element_text(size=14),
  legend.position = "right",
  plot.title = element_text(lineheight=.8,
                            face="bold", size = 16),
  panel.border = element_blank(),
  panel.grid.minor = element_blank(),
  panel.grid.major = element_blank(),
  axis.line.x = element_line(colour = 'black',
                             size=0.5, linetype='solid'),
  axis.line.y = element_line(colour = 'black',
                             size=0.5, linetype='solid'))

# Plot the thing
df %>% 
  mutate(Smoke = ifelse(Smoke == "yes",
                        "Smoker",
                        "Non-Smoker"),
         Exercise = ifelse(Exercise == "yes",
                           "Exercises",
                           "Doesn't Exercise"),
         Interaction = factor(str_replace(interaction(Smoke, Exercise),
                                          '\\.', '\n'),
                              ordered=TRUE)) %>% 
  ggplot(aes(x = Interaction, y = Happiness, fill = Smoke)) + 
  geom_flat_violin(position = position_nudge(x = .2, y = 0),
                   alpha = .8) +
  geom_point(aes(shape = Exercise),
             position = position_jitter(width = .05),
             size = 2, alpha = 0.8) +
  geom_boxplot(width = .1, outlier.shape = NA, alpha = 0.5) +
  coord_flip(xlim=c(1.25,4.25)) +
  labs(title = "Happiness by Smoking/Exercise",
       y = "Happiness") +
  scale_fill_discrete(guide = guide_legend(override.aes = list(shape = c(".", ".")))) +
  scale_shape_discrete(guide = guide_legend(override.aes = list(size = 3))) +
  theme_classic(base_size = 16) +
  theme(axis.title.x = element_blank()) +
  raincloud_theme
df <- data.frame("ID" = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                 "Happiness" = c(2, 3, 10, 7, 6, 8, 3, 9, 5, 1),
                 "Smoke" = c("yes", "yes", "no", "yes", "no", "no", "no", "no", "yes", "no"), 
                 "Exercise" = c("no", "yes", "no", "yes", "yes", "yes", "yes", "no", "no", "yes"))


ggplot(df, aes(x=Smoke, y=Happiness, fill = Smoke)) + 
  geom_boxplot(position = position_dodge()) +
  theme_classic() +
  labs(title = "Happiness by Smoking/Exercise", y = "Happiness") +
  geom_point(aes(shape = Exercise, colour = Exercise), position = position_dodge(width = 0.5)) +
  scale_shape_manual(values=c(17, 16)) +
  scale_color_manual(values = c("black", "blue"))