How to implement the "geom_density_ridges" function - r

I would like to generate with my data a similar plot as shown with this iris data set.
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
geom_density_ridges(aes(fill = Species)) +
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
Here you can see a cutout from my data set.
ratio = c(0, 0.05, 0.1, 0.15, 0.2, 0.25, 0, 0.05, 0.1, 0.15, 0.2, 0.25)
frequency = c(12000, 12300, 9000, 4300, 2434, 18000, 11000, 12200, 8000, 4100, 2400, 15900)
concentration = c("200", "200", "200", "200", "200", "200", "100", "100", "100", "100", "100", "100")
df = cbind(ratio, frequency, concentration)
View(df)
df = as.data.frame(df)
ggplot(df, aes(x = ratio, y = frequency)) +
geom_density_ridges(aes(fill = df$concentration)) +
scale_fill_manual(values = c("#00AFBB", "#E7B800"))
Unfortunately my code does not work. I don't know where my mistake is.

Is this what you're after?
library(ggplot2); library(ggridges)
ggplot(df, aes(x = ratio, y = concentration,
height = frequency/10000, fill = concentration)) +
geom_ridgeline() +
scale_fill_manual(values = c("#00AFBB", "#E7B800"))
data:
df = data.frame(stringsAsFactors = F,
ratio = c(0, 0.05, 0.1, 0.15, 0.2, 0.25, 0, 0.05, 0.1, 0.15, 0.2, 0.25),
frequency = c(12000, 12300, 9000, 4300, 2434, 18000, 11000, 12200, 8000, 4100, 2400, 15900),
concentration = c("200", "200", "200", "200", "200", "200", "100", "100", "100", "100", "100", "100"))

Related

Question regarding some specific labelling on plots

I have a code for a plot that I am trying to add specific labels for.
The data code is this:
CombinedMetricScore<-c("zero", "5", "10", "15", "20", "25", "30", "35", "40",
"45", "50", "60", "M11", "MICKEY", "MEANING", "MICKEYTWO",
"MICKEYTHREE", "MIKE", "PASTA", "MCIDandPASS",
"MICKDorPASS", "MIKEDOORPASS", "WOMAC20andPASS" ,"Ideal")
FalsePositiveRate<-c( 0, 0.05, 0.08, 0.12, 0.2, 0.28, 0.19, 0.5, 0.6, 0.7, 0.8, 0.94,
0.11, 0.28, 0.07, 0.5, 0.08, 0.28, 0.04, 0.3, 0.03, 0.03, 0.22, 1 )
TruePositiveRate<-c(0, 0.31, 0.35, 0.46, 0.69, 0.73, 0.59, 0.92, 0.92, 0.96, 1, 1,
0.46, 0.73, 0.42, 0.88, 0.35, 0.73, 0.46, 0.73, 0.46, 0.46, 0.69, 1)
ScoreOrMetric<-c("Metric", "Score", "Score", "Score", "Score", "Score", "Score", "Score", "Score",
"Score", "Score", "Score", "Metric", "Metric", "Metric", "Metric",
"Metric", "Metric", "Metric", "Metric",
"Metric", "Score", "Score", "Metric" )
COMBINEDSCORETABLE<-data.frame(CombinedMetricScore, FalsePositiveRate, TruePositiveRate, ScoreOrMetric)
The plot code is this:
ggplot(COMBINEDSCORETABLE, aes(x = FalsePositiveRate, y = TruePositiveRate, color = ScoreOrMetric)) +
geom_abline(slope = 1, intercept = .5, lwd = 1.5, color = "grey") +
geom_point(size =2, alpha = .8) +
coord_cartesian(xlim=c(0,1), ylim=c(0, 1)) +
coord_fixed() +
geom_text_repel(label = ifelse(TruePositiveRate > .44 + FalsePositiveRate,
yes = CombinedMetricScore, no = ""),
box.padding = 0.5)
Question: I want to add labels for the following 2 points "5", "45" but I don't know how to add it to my existing plot code.
We can use an | ("OR") in your ifelse logic. In general, though, I recommend only passing the data you need to geom_text_repel instead of everything (most of which having ""), so try this:
ggplot(COMBINEDSCORETABLE, aes(x = FalsePositiveRate, y = TruePositiveRate, color = ScoreOrMetric)) +
geom_abline(slope = 1, intercept = .5, lwd = 1.5, color = "grey") +
geom_point(size =2, alpha = .8) +
coord_cartesian(xlim=c(0,1), ylim=c(0, 1)) +
coord_fixed() +
ggrepel::geom_text_repel(
aes(label = CombinedMetricScore),
box.padding = 0.5,
data = ~ subset(., TruePositiveRate > (0.44 + FalsePositiveRate) | CombinedMetricScore %in% c("5", "45")))

Error in FUN(X[[i]], ...) : object 'estimate' not found

I am tryin to add p-values of 3 factors T-test on ggplot. But it keeps warning me this kind of error calls 'Error in FUN(X[[i]], ...) : object 'estimate' not found'. My coding is following:
Fisrt, I did a t-test
mixt.test_others<-studymix_fit %>%
group_by(reciprocity,stimu)%>%
t_test(Study_rate ~ estimate)%>%
adjust_pvalue()%>%
add_significance()
mixt.test_others1 <- mixt.test_others %>%
add_xy_position(x = "estimate", fun = "mean_sd", dodge = 0.8)
Then I add it to ggplot
ggplot(studymix_fit,aes(x=reciprocity, y=Study_rate,color = estimate,
fill = estimate))+
facet_grid(. ~ stimu)+
geom_violin(alpha = 0.1, adjust = 1.5)+
geom_boxplot(width = 0.1,alpha = 0.2,
position = position_dodge(width = 0.9))+
geom_dotplot(binaxis = "y", stackdir = 'center',
dotsize = 0.5, alpha = 0.5,
position = position_dodge(width = 0.9) )+
stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="pointrange",
color = "red", alpha = 1,width = 0.15,
position = position_dodge(width = 0.9))+
stat_compare_means(method = 'anova', label.y = 1.4)+
add_pvalue(mixt.test_others1,
label = "p = {p.adj.signif}",
tip.length = 0.01,
step.increase = 0.05,
y.position = 1.05)+
theme_classic()+
scale_fill_brewer(type = 'div', palette = 'Accent', direction = 1)+
scale_color_brewer(type = 'div', palette = 'Accent', direction = 1)+
labs(x="Reciprocity",y="Select Rate of Positive Reciprocity")
Here are part of my dataset:
structure(list(ID = c(102, 102, 102, 102, 103),
condition = c("A", "C", "B", "D", "A"),
Study_rate = c(1, 0, 1, 0, 0.666666667),
reciprocity = c("PS", "NS", "PS", "NS", "PS"),
estimate = c("PO", "PO", "NO", "NO", "PO"),
stimu = c("subject", "subject", "subject", "subject", "subject"))
Actually, I checked my original data set, it seems no problem.
I wanna know if I did the 3 factor t-test well?
Is there any other problem I made?
Please help me figure it out, thanks very much
If anyone has similar problems, we can disccus and figure them out~
Thanks

How do I scale the x-axis with ggplot?

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.
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)
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)

Plot zeros in a different colour to colour scale in ggplot2

I have a data frame, df:
df <- structure(list(animal = c("cat", "cat", "cat", "cat", "cat",
"cat", "cat", "cat", "cat", "cat", "cat", "cat", "cat", "cat",
"cat", "cat", "cat", "cat"), id = c("201", "202", "203", "204",
"215", "217", "201", "202", "203", "204", "215", "217", "201",
"202", "203", "204", "215", "217"), tissue = c("tail", "tail",
"tail", "tail", "tail", "tail", "whiskers", "whiskers", "whiskers",
"whiskers", "whiskers", "whiskers", "feet", "feet", "feet", "feet",
"feet", "feet"), value = c(0.5, 2.2, 0, 0.2, 0, 0, 2.8, 19.9,
0, 85, 0, 0, 1.9, 4.1, 0, 0.4, 0, 120)), row.names = c(NA, -18L
), class = "data.frame")
head(df)
animal id tissue value
1 cat 201 tail 0.5
2 cat 202 tail 2.2
3 cat 203 tail 0.0
4 cat 204 tail 0.2
5 cat 215 tail 0.0
6 cat 217 tail 0.0
I have the following plotting function:
p <- ggplot(df, aes_string(x = "id", y = "tissue", fill = "value")) +
geom_tile(color = "white") +
geom_text(aes_string(label = "value"), color = "black", size = 2) +
scale_fill_gradient(
low = "white", high = "red", na.value = "#D0D0D0") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
This produces:
I would like the zero values to be plotted in white, but any value above zero to start from an off-red (eg. #FEECE3).
I have tried this:
p <- ggplot(df, aes_string(x = "id", y = "tissue", fill = "value")) +
geom_tile(color = "white") +
geom_text(aes_string(label = "value"), color = "black", size = 2) +
scale_fill_gradient2(
low = "white", mid = "#FEECE3", high = "red", na.value = "#D0D0D0",
midpoint = 1) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
But it plots the zeros in the off-red colour.
How can I get just the zeros to plot in white?
You could use scale_fill_gradientn. Set your three colours to white, off-white and red, and set the values at which they reach these colors as 0, the smallest possible non-zero number, and 1.
ggplot(df, aes_string(x = "id", y = "tissue", fill = "value")) +
geom_tile(color = "white") +
geom_text(aes_string(label = "value"), color = "black", size = 2) +
scale_fill_gradientn(colours = c('white', "#FEECE3", 'red'),
values = c(0, .Machine$double.eps, 1)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

ggplotly with geom_rect not displaying geom_bar

I have a ggplot where I would like to have a striped background of grey and white. I have achieved this using geom_rect, as can be seen below:
ggplot(growth.mon, aes(x = Month, y = Rate)) +
geom_rect(ymin = 0.3, ymax = 0.4,
xmin = 0, xmax = 1000000, fill = '#fbfcfc') +
geom_rect(ymin = 0.2, ymax = 0.3,
xmin = 0, xmax = 1000000, fill = '#f5f6f9')+
geom_rect(ymin = 0.1, ymax = 0.2,
xmin = 0, xmax = 1000000, fill = '#fbfcfc')+
geom_rect(ymin = 0, ymax = 0.1,
xmin = 0, xmax = 1000000, fill = '#f5f6f9')+
geom_rect(ymin = -0.1, ymax = 0,
xmin = 0, xmax = 1000000, fill = '#fbfcfc')+
geom_rect(ymin = -0.2, ymax = -0.1,
xmin = 0, xmax = 1000000, fill = '#f5f6f9')+
geom_rect(ymin = -0.3, ymax = -0.2,
xmin = 0, xmax = 1000000, fill = '#fbfcfc')+
geom_bar(stat = "identity", aes(fill = as.factor(1)), show.legend = FALSE)+
geom_line(aes(y = rollMean, colour = "#7f5ba2"), size = 1.1, show.legend = FALSE)+
scale_fill_manual(values = c("#0095db"))+
scale_colour_manual(values = c("#7f5ba2"))+
scale_y_continuous(NULL, labels = percent_format())+
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y")+
theme(axis.text.x=element_text(angle=60, hjust=1))+
theme(legend.position = "none")
This creates this:
Now I am developing a shiny app and I would like this plot to be interactive rather than static, so I use ggplotly like so:
ggplotly(gg_growth)
However, the chart ends up removing the bars like this:
Can someone tell me what went wrong and how to fix this, please? Thank you.
Data:
dates <- seq(as.Date("2017-02-01"), length = 36, by = "1 month") - 1
sales_mon17 <- c(1503, 1563, 1434.5,1807, 1843.7, 1664, 1285, 1188, 1513, 1997,1718.2, 2191)
sales_mon18 <- c(1919, 1886, 1995, 1930, 1898, 2122, 1818, 1908, 1974, 2074, 1700, 2303)
sales_mon19 <- c(2319, 2424, 2353, 2474, 2500, 2538, 2444, 2219, 1908, 2404, 2288, 3079.7)
monthly_revenue <- data.frame(Month = dates, Revenue = c(sales_mon17, sales_mon18, sales_mon19))
growth.mon <- diff(monthly_revenue$Revenue) / lag(monthly_revenue$Revenue)[-1]
growth.mon <- data.frame(Month = monthly_revenue$Month[-1], Rate = growth.mon)
growth.mon$rollMean <- c(NA, NA, rollmean(growth.mon$Rate, 3))

Resources