How to reverse x-axis in box-plot [duplicate] - r

This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 1 year ago.
People, please can you evaluate my code and give me a hand?
I need to reverse de X-axis of my box-plot, as you can see, the x-axis is the age in Ma, R plot the values in ascending range and I need them in the reverse form (ie 65-64, 64 -63, 63-62, 62-61, 61-60). I tried with scale_y_reverse (), but no good results
Please help me.
Thanks a lot.Box-plot
Install Tidiverse
install.packages("tidyverse")
Open library
library(tidyverse)
Usign qplot
qplot(data =Filogen, x = Filogen$EDAD_1, y = Filogen$AREA_SUR, fill = Filogen$EDAD_1, geom = "boxplot", ylab = 'Área (km²)', xlab = 'Edad (Ma)')

A potential solution is to use the fct_rev() function from the forcats package (part of the tidyverse), for instance, using the example dataset from the palmerpenguins package:
# Load libraries
library(tidyverse)
library(palmerpenguins)
# Create a minimal reproducible example
MRE <- penguins %>%
na.omit()
# Plot the penguins data
qplot(data = MRE, x = species, y = bill_length_mm, fill = species, geom = "boxplot", ylab = 'Área (km²)', xlab = 'Edad (Ma)')
And, using fct_rev():
# Load libraries
library(tidyverse)
library(palmerpenguins)
# Create a minimal reproducible example
MRE <- penguins %>%
na.omit()
# Plot the penguins data
qplot(data = MRE, x = fct_rev(species), y = bill_length_mm, fill = species, geom = "boxplot", ylab = 'Área (km²)', xlab = 'Edad (Ma)')
This solution relies on "species" being a factor. In your dataset, the equivalent variable is "EDAD_1". If "EDAD_1" is not a factor, before you plot the data, change it to a factor:
Filogen$EDAD_1 <- factor(Filogen$EDAD_1)

Related

What's the easiest way to draw significance lines and asteriks in R?

So I have a good plot created in R consisting of 12 box plots. However, I need to do t-tests between them pair by pair and show their significance using the conventional bracketed lines and asterisks symbol. Is there no automated way to achieve this rather than using geom_line() manually to make every single line?
An example plot is shown below for what I would like to recreate except I'd like to do it for 12 plots instead of just 2.
I'm not sure about your data, but you may try using ggsignif package.
For an example data df,
library(dplyr)
library(ggsignif)
library(ggplot2)
library(reshape2)
df <- data.frame(
young = c(2:9),
old = seq(1, by = .5, length.out = 8)
)
df %>%
melt %>%
ggplot(aes(x = variable, y = value)) +
geom_boxplot() +
geom_signif(comparisons = list(c("young", "old")),
method = "t.test",
map_signif_level = TRUE)

ggplot2 version of shepard plot, i.e. vegan::stressplot()?

There seems to be quite a bit of information for plotting NMDS outputs (i.e. NMDS1 vs NMDS1) using ggplot2 however I cannot find a way to plot the vegan::stressplot() (shepard's plot) using ggplot2.
Is there a way to produce a ggplot2 version of a metaMDS output?
Reproducible code
library(vegan)
set.seed(2)
community_matrix = matrix(
sample(1:100,300,replace=T),nrow=10,
dimnames=list(paste("community",1:10,sep=""),paste("sp",1:30,sep="")))
example_NMDS=metaMDS(community_matrix, k=2)
stressplot(example_NMDS)
Created on 2021-09-17 by the reprex package (v2.0.1)
Here's a workaround to plot a very similar plot using ggplot2.The trick was to get the structure of the stressplot(example_NMDS) and extract the data stored in that object. I used the tidyverse package that includes ggplot and other packages such as tidyr that contains the pivot_longer function.
library(vegan)
library(tidyverse)
# Analyze the structure of the stressplot
# Notice there's an x, y and yf list
str(stressplot(example_NMDS))
# Create a tibble that contains the data from stressplot
df <- tibble(x = stressplot(example_NMDS)$x,
y = stressplot(example_NMDS)$y,
yf = stressplot(example_NMDS)$yf) %>%
# Change data to long format
pivot_longer(cols = c(y, yf),
names_to = "var")
# Create plot
df %>%
ggplot(aes(x = x,
y = value)) +
# Add points just for y values
geom_point(data = df %>%
filter(var == "y")) +
# Add line just for yf values
geom_step(data = df %>%
filter(var == "yf"),
col = "red",
direction = "vh") +
# Change axis labels
labs(x = "Observed Dissimilarity", y = "Ordination Distance") +
# Add bw theme
theme_bw()

ggplots2 combination of geom_line and geom_point creates too many shapes along lines

I am working on 3-way interaction effect plotting using my own data. But my code creates too many (continuous) shapes along the lines.
How can I leave the points only at the ends of the lines instead of the figure attached above?
I will deeply appreciate if anybody helps.
g1=ggplot(mygrid,aes(x=control,y=pred,color=factor(nknowledge),
lty=factor(nknowledge),shape=factor(nknowledge)))+
geom_line(size=1.5)+
geom_point(size=2.5)+
labs(x="control", y="attitudes",lty = "inc level")+
scale_linetype_manual("know level",breaks=1:3,values=c("longdash", "dotted","solid"),label=c("M-SD","M","M+SD"))+
scale_color_manual("know level",breaks=1:3,values=c("red", "blue","grey"),label=c("M-SD","M","M+SD"))+
scale_shape_manual("know level",breaks=1:3,values=c(6,5,4),label=c("M-SD","M","M+SD"))+
theme_classic()
This could be achieved by making use of a second dataset which filters the data for the endpoints by group using e.g. a group_by and range and passing the filtered dataset as data to geom_point:
Using some random example data try this:
set.seed(42)
mygrid <- data.frame(
control = runif(30, 1, 7),
pred = runif(30, 1, 3),
nknowledge = sample(1:3, 30, replace = TRUE)
)
library(ggplot2)
library(dplyr)
mygrid_pt <- mygrid %>%
group_by(nknowledge) %>%
filter(control %in% range(control))
ggplot(mygrid,aes(x=control,y=pred,color=factor(nknowledge),
lty=factor(nknowledge),shape=factor(nknowledge)))+
geom_line(size=1.5)+
geom_point(data = mygrid_pt, size=2.5)+
labs(x="control", y="attitudes",lty = "inc level")+
scale_linetype_manual("know level",breaks=1:3,values=c("longdash", "dotted","solid"),label=c("M-SD","M","M+SD"))+
scale_color_manual("know level",breaks=1:3,values=c("red", "blue","grey"),label=c("M-SD","M","M+SD"))+
scale_shape_manual("know level",breaks=1:3,values=c(6,5,4),label=c("M-SD","M","M+SD"))+
theme_classic()
If you use geom_point, then you'll get points for all rows in your data frame. If you want specific points and shapes plotted at the ends of your lines, you'll want to create a filtered data frame for the only points you want to have plotted.
library(ggplot2); library(dplyr)
g1 <- ggplot()+
geom_line(data = mtcars,
mapping = aes(x=hp,y=mpg,color=factor(cyl),lty=factor(cyl)),
size=1.5)+
geom_point(data = mtcars %>% group_by(cyl) %>% filter(hp == max(hp) | hp == min(hp)),
mapping = aes(x=hp,y=mpg,color=factor(cyl),shape=factor(cyl)),
size=2.5)
g1
Created on 2021-01-28 by the reprex package (v0.3.0)

Sort data in graph in R

I have the following code :
library(ggplot2)
ggplot(data = diamonds, aes(x = cut)) +
geom_bar()
with this result.
I would like to sort the graph on the count descending.
There are multiple ways of how to do it (it is probably possible just by using options within ggplot). But a way using dplyr library to first summarize the data and then use ggplot to plot the bar chart might look like this:
# load the ggplot library
library(ggplot2)
# load the dplyr library
library(dplyr)
# load the diamonds dataset
data(diamonds)
# using dplyr:
# take a dimonds dataset
newData <- diamonds %>%
# group it by cut column
group_by(cut) %>%
# count number of observations of each type
summarise(count = n())
# change levels of the cut variable
# you tell R to order the cut variable according to number of observations (i.e. count variable)
newData$cut <- factor(newData$cut, levels = newData$cut[order(newData$count, decreasing = TRUE)])
# plot the ggplot
ggplot(data = newData, aes(x = cut, y = count)) +
geom_bar(stat = "identity")

How to do stacked bar plot in R? (including the value of the var)

i need your help.
I was trying to do a stacked bar plot in R and i m not succeding for the moment. I have read several post but, no succed neither.
Like i am newbie, this is the chart I want (I made it in excel)
And this is how i have the data
Thank you in advance
I would use the package ggplot2 to create this plot as it is easier to position text labels than compared to the basic graphics package:
# First we create a dataframe using the data taken from your excel sheet:
myData <- data.frame(
Q_students = c(1000,1100),
Students_with_activity = c(950, 10000),
Average_debt_per_student = c(800, 850),
Week = c(1,2))
# The data in the dataframe above is in 'wide' format, to use ggplot
# we need to use the tidyr package to convert it to 'long' format.
library(tidyr)
myData <- gather(myData,
Condition,
Value,
Q_students:Average_debt_per_student)
# To add the text labels we calculate the midpoint of each bar and
# add this as a column to our dataframe using the package dplyr:
library(dplyr)
myData <- group_by(myData,Week) %>%
mutate(pos = cumsum(Value) - (0.5 * Value))
#We pass the dataframe to ggplot2 and then add the text labels using the positions which
#we calculated above to place the labels correctly halfway down each
#column using geom_text.
library(ggplot2)
# plot bars and add text
p <- ggplot(myData, aes(x = Week, y = Value)) +
geom_bar(aes(fill = Condition),stat="identity") +
geom_text(aes(label = Value, y = pos), size = 3)
#Add title
p <- p + ggtitle("My Plot")
#Plot p
p
so <- data.frame ( week1= c(1000,950,800), week2=c(1100,10000,850),row.names = c("Q students","students with Activity","average debt per student")
barplot(as.matrix(so))

Resources