Related
I'm struggling to get polar_coords to work as I had hoped. I want each item to be represented by a coloured track, with a range of 1:50000. I then wanted to plot points over these tracks at the corresponding locations, with symbols representing the different categories. The points would then be annotated with the id.
Dataframe:
structure(list(item = structure(c(1L, 2L, 2L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L), .Label = c("AA", "AB", "AC", "AD", "AE",
"BA", "BB", "BC", "BD", "BE"), class = "factor"), location = c(10045L,
12041L, 15035L, 22054L, 19023L, 49411L, 39012L, 3041L, 23065L,
33015L, 42069L, 26859L), category = structure(c(1L, 1L, 2L, 3L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L), .Label = c("X", "Y", "Z"), class = "factor"),
id = structure(c(1L, 8L, 2L, 7L, 6L, 10L, 5L, 1L, 1L, 3L,
4L, 9L), .Label = c("Apple", "Banana", "Cherry", "Grape",
"Mango", "Melon", "Orange", "Pear", "Raspberry", "Strawberry"
), class = "factor")), .Names = c("item", "location", "category",
"id"), class = "data.frame", row.names = c(NA, -12L))
my_data %>%
ggplot(aes(item, location, shape = category, label = id)) +
geom_col(aes(y = Inf), fill = "gray80") +
geom_point(size = 3) +
geom_text(vjust = -1) +
scale_x_discrete(expand = expand_scale(add = c(5,0))) +
coord_polar(theta = "y") +
theme_void()
If you want a break in the middle, you could change the item to a numeric value relating to it's desired position:
my_data %>%
mutate(item_pos = as.numeric(item),
item_pos = item_pos + if_else(item_pos > 5, 1, 0)) %>%
ggplot(aes(item_pos, location, shape = category, label = id)) +
...
Maybe you can work from this:
ggplot(data,aes(x=location, color=id, y=id)) +
geom_linerange(aes(y=id, xmin=0, xmax=50000, color=category), size=2, alpha=0.5) +
geom_point(size=3) +
coord_polar()
I've been unable to make my function work into R
Here are my test data:
df.summary <- structure(list(sample = structure(c(1L, 11L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L), .Label = c("P1",
"P10", "P11", "P12", "P13", "P14", "P15", "P16", "P18", "P19",
"P2", "P20", "P3", "P4", "P5", "P6", "P7", "P8", "P9"), class = "factor"),
my_col1 = c(0.18933457306591, 0.235931461802108, 0.189103550993512,
0.125949595916727, 0.0534753960389538, 0.147040309859083,
0.0911609796692189, 0.175136203125972, 0.116254981602728,
0.133480302179393, 0.109994771038499, 0.149204159468607,
0.105682126016057, 0.0967607072540045, 0.172893104456964,
0.115091434919033, 0.0653509609616037, 0.113300972345115,
0.0801326785643683), my_col2 = structure(c(1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("F", "M"), class = "factor"), my_col3 = c(0,
0, 0, 20.9715009722175, 13.3519208510716, 24.0257081096482,
19.2584928826721, 0, 0, 22.3923771843906, 16.6293335002717,
26.5622107372171, 0, 0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA,
-19L))
library(ggplot2)
## read data in
## df.summary <- read.csv('data_test.csv',header = TRUE,sep=';', check.names = FALSE)
plot_correlation <- function(my_df, my_col1, my_col3, my_col2, output) {
my_df[, my_col1] <- my_df[, my_col1] * 100
lm_plot <- ggplot(my_df, aes(my_col1, my_col3)) +
geom_point(data = my_df, aes(colour = my_col2), size = 2.5) +
scale_color_manual(values=c("violetred1", "royalblue1", "gold")) +
labs(x = "", y = "") +
geom_abline(intercept = 0, slope = 1,linetype="dotted") +
geom_smooth(data=subset(my_df, my_col2 == "M"),method="lm", color="royalblue1")
my_output <- output
ggsave(filename=my_output, plot=lm_plot,width = 9, height = 9, pointsize = 10)
}
plot_correlation(df.summary,'my_col1','my_col3','my_col2','test_outfig.pdf')
this code is giving me this plot:
When this code:
df.summary[,my_col1] <- df.summary[,my_col1]*100
ggplot(df.summary, aes(my_col1,my_col3)) +
geom_point(data = df.summary, aes(colour = my_col2), size = 2.5) +
scale_color_manual(values=c("violetred1", "royalblue1", "gold")) +
labs(x = "", y = "") +
geom_abline(intercept = 0, slope = 1,linetype="dotted") +
geom_smooth(data=subset(df.summary, my_col2 == "M"), method="lm", color="royalblue1")
Is giving me this plot (which is giving me exactly what I want):
It's looks like (maybe I'm wrong) inside the function, R is unable to link my col names and I can't figure out which is the right syntax ...
Replace aes with aes_string. Your code may somewhat work because the variable name (my_col1 etc) is exactly the variable value ("my_col1" etc). Since you want to specify column names using function arguments you'll need to either use tidyeval or use aes_string, which takes string values rather than unquoted symbols.
Also, there's no reason to copy output to my_output in the function body.
library("ggplot2")
df.summary <- structure(list(sample = structure(c(1L, 11L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L), .Label = c("P1",
"P10", "P11", "P12", "P13", "P14", "P15", "P16", "P18", "P19",
"P2", "P20", "P3", "P4", "P5", "P6", "P7", "P8", "P9"), class = "factor"),
my_col1 = c(0.18933457306591, 0.235931461802108, 0.189103550993512,
0.125949595916727, 0.0534753960389538, 0.147040309859083,
0.0911609796692189, 0.175136203125972, 0.116254981602728,
0.133480302179393, 0.109994771038499, 0.149204159468607,
0.105682126016057, 0.0967607072540045, 0.172893104456964,
0.115091434919033, 0.0653509609616037, 0.113300972345115,
0.0801326785643683), my_col2 = structure(c(1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("F", "M"), class = "factor"), my_col3 = c(0,
0, 0, 20.9715009722175, 13.3519208510716, 24.0257081096482,
19.2584928826721, 0, 0, 22.3923771843906, 16.6293335002717,
26.5622107372171, 0, 0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA,
-19L))
plot_correlation <- function(my_df, my_col1, my_col3, my_col2) {
my_df[, my_col1] <- my_df[, my_col1] * 100
ggplot(my_df, aes_string(my_col1, my_col3)) +
geom_point(data = my_df, aes(colour = my_col2), size = 2.5) +
scale_color_manual(values=c("violetred1", "royalblue1", "gold")) +
labs(x = "", y = "") +
geom_abline(intercept = 0, slope = 1,linetype="dotted") +
geom_smooth(data=subset(my_df, my_col2 == "M"),method="lm", color="royalblue1")
}
plot_correlation(df.summary,'my_col1','my_col3','my_col2')
Created on 2019-12-16 by the reprex package (v0.3.0)
Is there way to change colors of one bar( x - value) manualy in ggplot
data
for_plot_test=structure(list(name = c("A", "B",
"C", "A1", "A2", "A3",
"A4", "BI", "A", "B",
"C", "A1", "A2", "A3",
"A4", "BI"), n = c(1L, 3L, 5L, 7L, 9L, 11L, 13L, 15L, 2L, 4L, 6L, 8L, 10L, 12L, 14L, 16L),
value = c(0, 0.05, 0, 0.05, 0.05, 0.1, 0.05, 0, 1, 0.7, 0.6, 0.5, 0.4, 0.2, 0.2, 0.1),
variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
.Label = c("PROGRESS", "prev_progress"), class = "factor")),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -16L), vars = "name", labels = structure(list(name = c("Applications", "BI", "Clients", "CRE & Scoring", "Portfolio & Production", "SG Russia", "Transactions", "УКЛ & Prescoring")),
row.names = c(NA, -8L), class = "data.frame", vars = "name", drop = TRUE,
indices = list(0:1, 14:15, 6:7, 10:11, 2:3, 12:13, 8:9, 4:5),
group_sizes = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
biggest_group_size = 2L, .Names = "name"),
indices = list(c(0L, 8L), c(7L, 15L), c(3L, 11L), c(5L, 13L), c(1L, 9L), c(6L, 14L), c(4L, 12L), c(2L, 10L)),
drop = TRUE, group_sizes = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), biggest_group_size = 2L,
.Names = c("name", "n", "value", "variable"))
Current plot
colot_progress=c("#be877a","#dcbfad")
s <- ggplot(for_plot_test, aes(x= reorder(name, -n),y = value, fill = variable,label=ifelse(for_plot$value==0,"",scales::percent(for_plot$value))))+
geom_bar(stat='identity',position = "stack")+
scale_fill_manual(values=colot_progress,aesthetics = "fill")+
coord_flip()+
theme_minimal() + theme(
axis.title = element_blank(),
axis.text.x=element_blank(),
panel.grid = element_blank(),
legend.position="none"
)+
geom_text(size = 5, position = position_stack(vjust = 0.5))
s
Illustration of desire result
Creating another level for the column variable.
library(dplyr)
for_plot_test1 <-
for_plot_test %>%
group_by(name) %>%
summarise(n = n()) %>%
mutate(value = ifelse(name == "A", 1, 0), variable = "dummy") %>%
full_join(for_plot_test %>% mutate(value = replace(value, name == "A", 0)))
for_plot_test1$variable <- factor(for_plot_test1$variable,
levels = c("dummy", "PROGRESS", "prev_progress"))
colot_progress <- c("limegreen", "#be877a", "#dcbfad")
s <- ggplot(for_plot_test1,
aes(
x = reorder(name,-n),
y = value,
fill = variable,
label = ifelse(value == 0, "", scales::percent(value))
)) +
geom_bar(stat = 'identity', position = "stack") +
scale_fill_manual(values = colot_progress, aesthetics = "fill") +
coord_flip() +
theme_minimal() + theme(
axis.title = element_blank(),
axis.text.x = element_blank(),
panel.grid = element_blank(),
legend.position = "none"
) +
geom_text(size = 5, position = position_stack(vjust = 0.5))
s
this is my dataset:
> dput(dfw)
structure(list(SITE = c("ASPEN", "ASPEN", "BioCON", "DUKE", "Lancaster",
"Merrit Island", "Nevada FACE", "NZ", "ORNL", "PHACE", "BioCON"
), SPECIES = c("A", "AB", "Legume", "PITA", "mixed", "Oak", "desert",
"grassland", "SG", "grassland", "C3forb"), FRr = c(0.197028535345918,
0.296799297050907, 0.195436310641759, 0.152972526753089, 0.0313948973476966,
0.139533057346518, 0.188221278921143, NA, 0.70542764380006, 0.119320766735777,
0.135665667633474), Nupr = c(0.122177669046786, 0.305573297532757,
0.131181914007488, 0.217519050530067, -0.0436788294371676, 0.153632658941404,
-0.00803217169726427, 0.168440046857285, 0.145172439177718, -0.108563178158001,
0.00546006390438276), myc = c("ECM", "ECM", "N-fixing", "ECM",
"ECM", "ECM", "AM", "AM", "AM", "AM", "AM"), SITE_Sps = structure(c(1L,
2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 3L), .Label = c("Aspen FACE-A",
"Aspen FACE-AB", "BioCON", "BioCON-legumes", "Duke FACE", "Lascaster",
"Florida OTC", "Nevada FACE", "NZ FACE", "ORNL FACE", "PHACE"
), class = "factor")), row.names = c(NA, -11L), vars = list(SITE,
SPECIES, myc), indices = list(0L, 1L, 10L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
SITE = c("ASPEN", "ASPEN", "BioCON", "BioCON", "DUKE", "Lancaster",
"Merrit Island", "Nevada FACE", "NZ", "ORNL", "PHACE"), SPECIES = c("A",
"AB", "C3forb", "Legume", "PITA", "mixed", "Oak", "desert",
"grassland", "SG", "grassland"), myc = structure(c(2L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("am", "ecm",
"ecm+am"), class = "factor")), row.names = c(NA, -11L), class = "data.frame", vars = list(
SITE, SPECIES, myc), .Names = c("SITE", "SPECIES", "myc")), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), .Names = c("SITE", "SPECIES",
"FRr", "Nupr", "myc", "SITE_Sps"))
I want to draw the same background as in the attached figure, added to my current ggplot code:
ggplot(dfw, aes(FRr, Nupr, group=myc, label = SITE_Sps)) +
geom_point(aes(fill=myc),size=4,shape = 21) +
geom_text() +
geom_hline(yintercept=0) + geom_vline(xintercept = 0) +
geom_abline(intercept = 0, slope = 1, linetype = "longdash")
I guess I should use the function geom_polygon, but I don't really know how to create a dataset to draw all the required segments, including the colour gradient from dark grey to light grey and white.
Perhaps this could be a start?
nlines <-
phis <- seq( 0, 2*pi, by=2*pi/nlines )
rad <- 999
xs <- rad * cos( phis )
ys <- rad * sin( phis )
Here is a way using geom_polygon:
nlines <- 25
inc <- pi/(nlines)
phis <- seq( -pi/2, by=inc, length.out = nlines )
rad <- 1
#Create the triangles
points <- lapply(phis, function(a) {
x <-c(0, rad*cos(a), rad*cos(a+inc),0, -rad*cos(a), -rad*cos(a+inc))
y <-c(0, rad*sin(a), rad*sin(a+inc),0, rad*sin(a), rad*sin(a+inc))
g <-c(a,a,a,a,a,a) # used for grouping
data.frame(x,y,g)
})
#Create a data.frame to be used on ggplot
bckg <- do.call(rbind,points)
#You need to set the data for each geometry as we have more than one dataset
ggplot(mapping=aes(FRr, Nupr, group=myc)) +
#Draw the background
geom_polygon(data=bckg,aes(x=x,y=y,group=g,alpha=g), fill = "gray50")+
geom_point(data=dfw, aes(FRr, Nupr, group=myc, fill=myc),size=4,shape = 21) +
geom_text(data=dfw, aes(FRr, Nupr, group=myc, label = SITE_Sps), nudge_y = -0.02) +
geom_hline(data=dfw,yintercept=0) + geom_vline(data=dfw,xintercept = 0) +
geom_abline(data=dfw,intercept = 0, slope = 1, linetype = "longdash")+
#We need to define a scale in ourder to deal with out of boundary points on the background
scale_x_continuous(limits = c(-0.2,0.4), oob=function(x, rg) x)+
scale_y_continuous(limits = c(-0.2,0.4), oob=function(x, rg) x)+
scale_alpha_continuous(guide="none", range=c(1.0,0))+
theme(panel.background = element_blank())
Here is the plot:
This might seem a really stupid mistake on my part but whenever I specify geom_point depending on a factor and choose a hollow point and a solid point (shapes 1 and 19), and plot error bars, it crosses the point.
Here are my data frames:
> dput(head(allbins.sum))
structure(list(T = c(0L, 0L, 10L, 10L, 20L, 20L), treatment = structure(c(1L,
2L, 1L, 2L, 1L, 2L), .Label = c("control bead", "dP bead"), class = "factor"),
N = c(3, 3, 3, 3, 3, 3), cellsBase = c(0, 0, 0.013028995209506,
0.135599858885737, -0.0130289952095061, 0.759359209760127
), sd = c(0, 0, 0.0597063567767786, 0.0469731690178533, 0.0983667566897066,
0.183436089048999), se = c(0, 0, 0.034471481157405, 0.0271199717771474,
0.0567920734541125, 0.105906875391532), ci = c(0, 0, 0.148318812500416,
0.116687820597672, 0.244356569875469, 0.455680506502609),
bin = c("BinA", "BinA", "BinA", "BinA", "BinA", "BinA")), .Names = c("T",
"treatment", "N", "cellsBase", "sd", "se", "ci", "bin"), row.names = c(NA,
6L), class = "data.frame")
> dput(head(allbins.fitdata))
structure(list(wellvidbin = structure(c(1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("A1-002-BinA", "A1-002-BinB", "A1-002-BinC", "A1-031-BinA",
"A1-031-BinB", "A1-031-BinC", "A3-004-BinA", "A3-004-BinB", "A3-004-BinC",
"B1-032-BinA", "B1-032-BinB", "B1-032-BinC", "B4-026-BinA", "B4-026-BinB",
"B4-026-BinC", "C4-027-BinA", "C4-027-BinB", "C4-027-BinC"), class = "factor"),
treatment = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("control bead",
"dP bead"), class = "factor"), wellvid = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("A1-002", "A1-031", "A3-004",
"B1-032", "B4-026", "C4-027"), class = "factor"), bin = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("BinA", "BinB", "BinC"), class = "factor"),
T = c(0L, 10L, 20L, 30L, 40L, 50L), T.factor = structure(1:6, .Label = c("0",
"10", "20", "30", "40", "50", "60"), class = "factor"), cells = c(7L,
11L, 26L, 27L, 28L, 36L), cellsS = c(-1.36568429306349, -1.20296446240061,
-0.592765097414793, -0.552085139749072, -0.511405182083351,
-0.185965520757582), cellsBase = c(0, 0.162719830662884,
0.772919195648701, 0.813599153314422, 0.854279110980143,
1.17971877230591), treatT = structure(c(2L, 4L, 6L, 8L, 10L,
12L), .Label = c("control bead.0", "P bead.0", "control bead.10",
"P bead.10", "control bead.20", "P bead.20", "control bead.30",
"P bead.30", "control bead.40", "P bead.40", "control bead.50",
"P bead.50", "control bead.60", "P bead.60"), class = "factor"),
fit = c(0.0285939715820639, 0.304399288764407, 0.58020460594675,
0.856009923129092, 1.13181524031144, 1.40762055749378), se.fit = c(0.157415367032567,
0.132348142293459, 0.114707848741265, 0.108190467052118,
0.114707848741265, 0.132348142293459), upr = c(0.337128090965895,
0.563801647659587, 0.805031989479629, 1.06806323855124, 1.35664262384431,
1.66702291638896), lwr = c(-0.279940147801767, 0.0449969298692267,
0.35537722241387, 0.643956607706942, 0.906987856778556, 1.1482181985986
)), .Names = c("wellvidbin", "treatment", "wellvid", "bin",
"T", "T.factor", "cells", "cellsS", "cellsBase", "treatT", "fit",
"se.fit", "upr", "lwr"), class = c("data.table", "data.frame"
), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x0000000000100788>)
And the code:
ggplot(data=allbins.sum, aes(x=T, y=cellsBase, shape=treatment)) + geom_point(size=5, aes(shape=treatment))+
geom_errorbar(aes(ymin=cellsBase-se, ymax=cellsBase+se), width=2, size=1) +
geom_smooth(data=allbins.fitdata, size=1, aes(y=fit, ymin=lwr, ymax=upr),
color="black", method="lm", stat="identity", alpha=0.2)+
facet_grid(bin~.) +
scale_shape_manual(values=c(1, 19))
This gives me this plot:
Any hints on how to have the hollow circles to be hollowed?
I also tried specifying geom_shape (aes(fill=treatment) and then scale_fill_manual but then it is also applied to my geom_smooth
Thanks for the help!
If you mean that you don't want the line of the error bar to be visible through the 'hollow' points, then plot geom_errorbar first, then plot geom_point second, with solid fill, so it will overlay the error bar.
ggplot(data=allbins.sum, aes(x=T, y=cellsBase)) +
# plotting this first
geom_errorbar(aes(ymin=cellsBase-se, ymax=cellsBase+se), width=2, size=1) +
# plotting this second, with a hollow fillable shape, and black outline
geom_point(size=5, shape = 21, color='black',
aes(fill = treatment)) +
# solid black and solid white fill for the points
scale_fill_manual(values = c('black', 'white')) +
theme_bw()
(The data you posted only has these points for allbins.sum, and the code for allbins.fitdata has an error, so no error bars on this plot)