admin管理员组

文章数量:1389903

I'm trying to create a scatter plot using ggplot2 in R where each point is connected by a line within its group. The data represents different conditions affecting various outcomes, and each condition should have its own color and line connecting its points. The challenge is ensuring that the lines connect the dots correctly after applying position dodging to prevent overlapping of points.

Here's a simplified version of the dataset and the code I'm using:

    # Sample data
set.seed(123)
example_data <- data.frame(
  condition = rep(c("Condition A", "Condition B", "Condition C"), each = 3),
  outcome = rep(c("Outcome 1", "Outcome 2", "Outcome 3"), 3),
  estimate = runif(9, -2, 2),
  error = runif(9, 0.5, 1.5)
)

example_data$lower <- example_data$estimate - example_data$error
example_data$upper <- example_data$estimate + example_data$error

# Attempt to plot
library(ggplot2)
ggplot(example_data, aes(x = estimate, y = outcome, group = interaction(condition, outcome), color = condition)) +
  geom_point(position = position_dodge(width = 0.5), size = 3) +
  geom_line(aes(group = condition), position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(xmin = lower, xmax = upper), width = 0.1, position = position_dodge(width = 0.5)) +
  theme_minimal() +
  labs(title = "Effect of Different Conditions on Various Outcomes", x = "Coefficient Estimate", y = "Outcome") +
  scale_color_manual(values = hcl.colors(3, "Berlin"))

Right now, it looks like the following. The issue with this is that the dots are not connected to the line.

I'm trying to create a scatter plot using ggplot2 in R where each point is connected by a line within its group. The data represents different conditions affecting various outcomes, and each condition should have its own color and line connecting its points. The challenge is ensuring that the lines connect the dots correctly after applying position dodging to prevent overlapping of points.

Here's a simplified version of the dataset and the code I'm using:

    # Sample data
set.seed(123)
example_data <- data.frame(
  condition = rep(c("Condition A", "Condition B", "Condition C"), each = 3),
  outcome = rep(c("Outcome 1", "Outcome 2", "Outcome 3"), 3),
  estimate = runif(9, -2, 2),
  error = runif(9, 0.5, 1.5)
)

example_data$lower <- example_data$estimate - example_data$error
example_data$upper <- example_data$estimate + example_data$error

# Attempt to plot
library(ggplot2)
ggplot(example_data, aes(x = estimate, y = outcome, group = interaction(condition, outcome), color = condition)) +
  geom_point(position = position_dodge(width = 0.5), size = 3) +
  geom_line(aes(group = condition), position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(xmin = lower, xmax = upper), width = 0.1, position = position_dodge(width = 0.5)) +
  theme_minimal() +
  labs(title = "Effect of Different Conditions on Various Outcomes", x = "Coefficient Estimate", y = "Outcome") +
  scale_color_manual(values = hcl.colors(3, "Berlin"))

Right now, it looks like the following. The issue with this is that the dots are not connected to the line.

Share Improve this question asked Mar 15 at 0:27 paul_on_pcpaul_on_pc 1511 gold badge1 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

The native ggplot2 option would be to explicitly set orientation="y" for geom_line to dodge the lines in the y or vertical direction which as a side effect will connect the points according to order of the variable mapped on y:

library(ggplot2)

ggplot(example_data, aes(
  x = estimate, y = outcome,
  group = interaction(condition, outcome),
  color = condition
)) +
  geom_point(
    position = position_dodge(width = 0.5), size = 3
  ) +
  geom_line(
    aes(group = condition),
    position = position_dodge(width = 0.5),
    orientation = "y"
  ) +
  geom_errorbar(aes(xmin = lower, xmax = upper),
    width = 0.1,
    position = position_dodge(width = 0.5)
  ) +
  theme_minimal() +
  labs(
    title = "Effect of Different Conditions on Various Outcomes",
    x = "Coefficient Estimate", y = "Outcome"
  ) +
  scale_color_manual(values = hcl.colors(3, "Berlin"))

I'm not sure position_dodge currently has an option to make geom_line dodge vertically instead of horizontally. One option adapted from Vertical equivalent of position_dodge for geom_point on categorical scale is to use ggstance::position_dodgev():

geom_line(aes(group = condition), position = ggstance::position_dodgev(height = 0.5)) +

本文标签: rHow to Correctly Connect Lines with Dots in a ggplot2 Plot with DodgingStack Overflow