logo
down
shadow

R ggplot: How to create a scatter plot with marginal box plots


R ggplot: How to create a scatter plot with marginal box plots

By : user2187150
Date : November 26 2020, 04:01 AM
hope this fix your issue There is more than way to create scatter plots with marginal boxplots. I know of 2 at least: one with ggplot and ggExtra, and another one with the package car:
your data:
code :
d1 <- read.table(header = TRUE, text =
    "a1c   att  status
    8.500000 23.58333  case
    8.450000 12.25000 control
    8.266667 18.91667 control")
# load the packages
library(ggplot2)
library(ggExtra)

# make a usual ggplot and store it
# point size increased, legend to the bottom
p1 <- ggplot(d1, aes(x=a1c, y=att , color=status)) +
  geom_point(size=2.5) +
  theme(legend.position="bottom")

# marginal boxplot
# relative size of the central plot increased
ggMarginal(p1, type="boxplot", size=7)
library(car)
scatterplot(d1$a1c ~ d1$att | d1$status, 
            boxplots = "xy", regLine=FALSE, fill=d1$status, cex=2)


Share : facebook icon twitter icon
How to create 4x4 scatter plot using GGPLOT Facet

How to create 4x4 scatter plot using GGPLOT Facet


By : Ashutosh Kumar Singh
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have data frame like this:
code :
library(ggplot2)
d.405 <- data.frame(abs(rnorm(30)),abs(rnorm(30)),abs(rnorm(30)),abs(rnorm(30)),type="405")
d.409 <- data.frame(abs(rnorm(30)),abs(rnorm(30)),abs(rnorm(30)),abs(rnorm(30)),type="409")
all <- rbind(d.405,d.409)
colnames(all) <- c("401","402","403","404","type")

library("reshape2");
allM <- melt(all, id.vars = "type")


combis <- expand.grid(levels(allM$variable),levels(allM$variable))

plotdat <- lapply(seq_len(nrow(combis)),function(i) cbind(allM[allM$variable==combis[i,1] & allM$type=="405",],
                                               allM[allM$variable==combis[i,2] & allM$type=="409",c("type","variable","value")]))
plotdat <- do.call(rbind,plotdat)
names(plotdat) <- c("type.x","var.x","x","type.y","var.y","y")
plotdat$var.x <- paste("x:",plotdat$var.x)
plotdat$var.y <- paste("y:",plotdat$var.y)

library(plyr)
cors <- ddply(plotdat,.(var.x,var.y),summarize,cor=format(signif(cor(x,y),2),scientific=-2))
cors$x <- 2.2
cors$y <- 2.5

p <- ggplot(plotdat,aes(x=x,y=y)) + 
  geom_point() + 
  geom_smooth(method="lm") +
  geom_text(data=cors,aes(label=paste("r =",cor))) +
  facet_wrap(~var.y*var.x,ncol=4) +
  xlab("405") + ylab("409")


print(p)
GGplot does not change color of scatter plot but does on other plots

GGplot does not change color of scatter plot but does on other plots


By : Raffy
Date : March 29 2020, 07:55 AM
hope this fix your issue in this line
geom_point(aes(colour=ifelse( data3$LOF>lof & data3$Z_LAST_DAYS100,"#8e8f90","#f40009")), size = 3)
code :
ggplot(iris, aes(Sepal.Width, Sepal.Length)) + 
  geom_point(aes(colour="pink"))
ggplot(iris, aes(Sepal.Width, Sepal.Length)) + 
  geom_point(colour="pink")
geom_point(colour=ifelse( data3$LOF>lof &  data3$Z_LAST_DAYS<days &  data3$CE11000_ERLOS>100,"#8e8f90","#f40009"), size = 3)
ggExtra plot format: similar marginal plots for different plot dimensions

ggExtra plot format: similar marginal plots for different plot dimensions


By : Sachin Gupta
Date : March 29 2020, 07:55 AM
like below fixes the issue I suggest the axis_canvas function from the cowplot package. (Disclaimer: I'm the package author.) It requires a little more work, but it allows you to draw any marginals you want. And you can specify the size exactly, in output units (e.g. inch).
code :
require(cowplot)

pmain <- ggplot(data = mpg, aes(x = cty, y = hwy)) + 
  geom_point() + 
  xlab("City driving (miles/gallon)") +
  ylab("Highway driving (miles/gallon)")

xbox <- axis_canvas(pmain, axis = "x", coord_flip = TRUE) + 
  geom_boxplot(data = mpg, aes(y = cty, x = 1)) + coord_flip()
ybox <- axis_canvas(pmain, axis = "y") + 
  geom_boxplot(data = mpg, aes(y = hwy, x = 1))

p1 <- insert_xaxis_grob(pmain, xbox, grid::unit(1, "in"), position = "top")
p2 <- insert_yaxis_grob(p1, ybox, grid::unit(1, "in"), position = "right")
ggdraw(p2)
require(cowplot)

pmain <- ggplot(data = mpg, aes(x = cty, y = hwy, color = factor(cyl))) + 
  geom_point() + 
  xlab("City driving (miles/gallon)") +
  ylab("Highway driving (miles/gallon)") +
  theme_minimal()

xbox <- axis_canvas(pmain, axis = "x", coord_flip = TRUE) + 
  geom_boxplot(data = mpg, aes(y = cty, x = factor(cyl), color = factor(cyl))) + 
  scale_x_discrete() + coord_flip()
ybox <- axis_canvas(pmain, axis = "y") + 
  geom_boxplot(data = mpg, aes(y = hwy, x = factor(cyl), color = factor(cyl))) +
  scale_x_discrete()

p1 <- insert_xaxis_grob(pmain, xbox, grid::unit(1, "in"), position = "top")
p2 <- insert_yaxis_grob(p1, ybox, grid::unit(1, "in"), position = "right")
ggdraw(p2)
Marginal plots using axis_canvas in cowplot: How to insert gap between main panel and marginal plots

Marginal plots using axis_canvas in cowplot: How to insert gap between main panel and marginal plots


By : A. Varukhin
Date : March 29 2020, 07:55 AM
it should still fix some issue The following came up in a comment to this post: When making marginal plots with the axis_canvas() function in cowplot, how can we create a gap between the main plot and the marginal plot? , I see two options:
Insert empty plot
code :
# pmain, xbox, ybox are defined as in the question
pnull <- ggdraw() # generate empty plot

p1 <- insert_xaxis_grob(
        insert_xaxis_grob(pmain, xbox, grid::unit(0.6, "in"), position = "top"),
        pnull, grid::unit(0.2, "in"), position = "top")

p2 <- insert_yaxis_grob(
        insert_yaxis_grob(p1, ybox, grid::unit(0.6, "in"), position = "right"),
        pnull, grid::unit(0.2, "in"), position = "right")
ggdraw(p2)
xbox2 <- axis_canvas(pmain, axis = "x", coord_flip = TRUE) + 
  geom_boxplot(data = mpg, aes(y = cty, x = as.numeric(factor(cyl)), color = factor(cyl))) + 
  scale_x_continuous(limits = c(-2, 4.5)) + coord_flip()
ybox2 <- axis_canvas(pmain, axis = "y") + 
  geom_boxplot(data = mpg, aes(y = hwy, x = as.numeric(factor(cyl)), color = factor(cyl))) +
  scale_x_continuous(limits = c(-2, 4.5))

p1 <- insert_xaxis_grob(pmain, xbox2, grid::unit(0.8, "in"), position = "top")
p2 <- insert_yaxis_grob(p1, ybox2, grid::unit(0.8, "in"), position = "right")
ggdraw(p2)
plot_grid(xbox + panel_border("black"), 
          xbox2 + panel_border("black"), nrow = 1, scale = 0.9)
How to create two lines and scatter plots using ggplot

How to create two lines and scatter plots using ggplot


By : user1128882
Date : March 29 2020, 07:55 AM
Does that help I have the following data in R: , How about something like this?
code :
data %>%
    gather(k, value, -id) %>%
    mutate(
        state = gsub("(\\.e$|\\.f$)", "", k),
        what = gsub("(initial\\.|final\\.)", "", k)) %>%
    ggplot(aes(id, value, colour = what)) +
    geom_line() +
    facet_wrap(~ state)
data %>%
    gather(k, value, -id) %>%
    mutate(
        state = gsub("(\\.e$|\\.f$)", "", k),
        what = gsub("(initial\\.|final\\.)", "", k)) %>%
    ggplot(aes(id, value, colour = what)) +
    geom_line() +
    geom_point() + 
    facet_wrap(~ state)
data %>%
    gather(k, value, -id) %>%
    mutate(
        state = gsub("(\\.e$|\\.f$)", "", k),
        what = gsub("(initial\\.|final\\.)", "", k)) %>%
    select(-k) %>%
    spread(state, value) %>%
    ggplot(aes(x = initial, y = final, colour = what, fill = what)) +
    geom_smooth(fullrange = T, method = "lm") +
    geom_point()
Related Posts Related Posts :
  • Finding income ratio by student status
  • Sum and Count Changes per Group for each Column in R
  • Writing a Path/Route Plot as a GeoTiff in R
  • Shorter order expression in R
  • Make a function using apply, stringr, stringi, and rbind run faster
  • Using cast() or ddply() to summarise the mean for two continuous variables in one dataframe
  • How to assign ID to multiple rows based on a value in 1 column in 1 row duplicating a value in a DIFFERENT column in a d
  • apply transparent background to divide plot area based on x values using ggplot
  • pivot a data frame and exclude blank cells in r
  • Subsetting Polygons from Spatial Polygons object by slot
  • Error: Don't know how to add e2 to a plot
  • selecting values from a df based on multiple percentages from a different dataframe
  • Why does R paste more decimals than have been rounded, but only sometimes?
  • Emacs ESS indent after %>%
  • Editing data frame after reactive upload in R shiny
  • R: How to add a zero into the middle of a string
  • Write a loop to select all combination of variable values generating positive equation values in R
  • Parameterizing group_by %>% summarise
  • How to display HTML in DT header?
  • R Vectorizing Operation
  • Cystoscape node color
  • geom_tile border missing at corners
  • Geting new data into old data frame in R
  • Eigenvector values for different time periods of same network (igraph in R)
  • Write a function with default column name inputs in dplyr::mutate()
  • Why isn't string splitting after last open parenthesis?
  • blogdown - how do I specify which page a post will appear on
  • Can I span groups of categories with horizontal lines in ggplot2
  • RODBC gives proper row count but yields empty query
  • Merging two incomplete factors
  • reshaping data with time represented as spells
  • Rcpp use outer with pmax
  • Symbolic matrix mutiplication error (Ryacas)
  • Creating columns based on total number of columns in a data frame R
  • How do I address R raster mosaic error: 'data' must be of a vector type, was 'NULL'?
  • ggplot2() plotting one variable against itself by factor?
  • Random sets with three random numbers in it (sampling random points in a cube)
  • Function to find varying strings
  • How do I post some introductory paragraphs on the main page of my blogdown site?
  • Correlations between numerous variables grouped in dplyr
  • Animating 3d object in R Markdown html with play3d
  • Suppress multiple package/library loading messages
  • R CMD REMOVE has no effect
  • repeated observations average per month
  • Count values per year and based on other column
  • aggregating elements to create groups of minimal size
  • efficient subsetting of data.table with greater-than, less-than using indices
  • Prefix/suffix column content with column names
  • ggplot and ggsignif error on grouping variable
  • R - How to use sum and group_by inside apply?
  • For Each Loop to convert into numeric values
  • How to summarise taking a random value from a categorical column?
  • Predictions in SageMaker ::: Writing Function To Split Big Data-frame Into Batches For Predictions
  • ggplot: How to keep marker colours in legend but hide text colours?
  • Removing character elements from a vector
  • Cumulative sum based on factor on R
  • expand colnames to match the last known one
  • `testthat::expect_silent()` does not seem to notice ggplot2 errors
  • Parse Factor in R
  • Exit from Command prompt after running r script
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org