1

I used the dataset below to make a plot. However, I don't know how to scale the x-axis and make it look nice.

Dataset
Plot

ggplot(data = ggplot_data, mapping = aes(x = Estimate, y = Phenotype, group = Estimate_type,color = Estimate_type))+ 
  geom_pointrange(aes(xmin = `Lower CI`, xmax = `Upper CI`), position = position_dodge(width = 0.25)) + 
  coord_cartesian(xlim = c(2.0,  20.0))+  
  labs(color = "Estimate Type") +
  ggtitle("Within- and Between-Family Prediction Estimates")
ggsave("Estimateplot.png", width = 15, height = 5)
Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • 1
    It looks like your “estimate” value maybe a character and not a number. Check that first, second remove the “coord_cartesian” statement, you shouldn’t need it. – Dave2e Jul 07 '22 at 00:20
  • If you use “dput()” to provide a sample of the data, it would be easier to help. A picture of the data rarely does. – Dave2e Jul 07 '22 at 00:22

1 Answers1

0

Perhaps try changing x = Estimate to x = as.numeric(Estimate)? E.g.

library(ggplot2)

ggplot_data <- data.frame(...1 = c("ASD Within", "ASD Between", "ADHD Within", "ADHD Between"),
                          "Estimate" = c(0.08747, 0.0208, 0.1805, 0.09616),
                          "Lower CI" = c(0.015, -0.03, 0.11, 0.04),
                          "Upper CI" = c(0.15, 0.72, 0.24, 0.14),
                          "Phenotype" = c("Autism Score", "Autism Score",
                                        "ADHD Score", "ADHD Score"),
                          "Estimate_type" = c("Within Family", "Between Family",
                                            "Within Family", "Between Family"),
                          check.names = FALSE)

ggplot(data = ggplot_data, mapping = aes(x = as.numeric(Estimate), y = Phenotype, group = Estimate_type, color = Estimate_type))+ 
  geom_pointrange(aes(xmin = `Lower CI`, xmax = `Upper CI`), position = position_dodge(width = 0.25)) + 
  #coord_cartesian(xlim = c(2.0,  20.0))+  
  labs(color = "Estimate Type") +
  ggtitle("Within- and Between-Family Prediction Estimates")

Created on 2022-07-07 by the reprex package (v2.0.1)

Edit

Not sure if it's necessary, but you can also change the orientation of the keys in the legend using:

library(tidyverse)

ggplot_data <- data.frame(...1 = c("ASD Within", "ASD Between", "ADHD Within", "ADHD Between"),
                          "Estimate" = c(0.08747, 0.0208, 0.1805, 0.09616),
                          "Lower CI" = c(0.015, -0.03, 0.11, 0.04),
                          "Upper CI" = c(0.15, 0.72, 0.24, 0.14),
                          "Phenotype" = c("Autism Score", "Autism Score",
                                        "ADHD Score", "ADHD Score"),
                          "Estimate_type" = c("Within Family", "Between Family",
                                            "Within Family", "Between Family"),
                          check.names = FALSE)

# Custom Key Glyph
draw_key_hpointrange <- function(data, params, size) {
  grid::grobTree(
    draw_key_path(data, params, size),
    draw_key_point(transform(data, 
                             size = (data$size %||% 1.5) * 4),
                   params)
  )
}

ggplot(data = ggplot_data, mapping = aes(x = as.numeric(Estimate), y = Phenotype, group = Estimate_type, color = Estimate_type))+ 
  geom_pointrange(aes(xmin = `Lower CI`, xmax = `Upper CI`),
                  position = position_dodge(width = 0.25), 
                  key_glyph = "hpointrange") + 
  #coord_cartesian(xlim = c(2.0,  20.0))+  
  labs(color = "Estimate Type") +
  ggtitle("Within- and Between-Family Prediction Estimates")

Created on 2022-07-07 by the reprex package (v2.0.1)

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46