Error with legend of ggplot graph - r

I have a data set with three variables: Index of Economic Freedom, Total GDP and GDP per capita of ten countries.
I am trying to plot a graph, in which the size of the circles should be proportional to the GDP of each country.
However, on the right side of the graph, I am getting these blue squares instead of circles with different sizes.
How can I get rid of these blue squares and have R to display the circles with different sizes instead?
Here is my result:
Here is the data and the code I am using:
score <- c(60,65.7,48.9,47.9,44.1,67.1,81.4,71.2,61.0,68.7)
gdp <- c(35.146, 26.499,285.541,130.066,777.945, 20.352,999.595,361.402,102.811,11.411)
gdp.percapita <- c(1150.36,9506.12,7534.06,6247.28,18749.34,6190.75,43073.06,42596.55,11044.25,32036.09)
x <- data.frame(score, gdp, gdp.percapita)
p <- ggplot(data=x,aes(x= score, y= gdp.percapita, size = gdp))
p <- p + theme(axis.title.x = element_text(face="bold", size=20)) +
labs(x="Economic Index Freedom")
p <- p + theme(axis.title.y = element_text(face="bold", size=20)) +
labs(y="GDP per Capita")
p + geom_point(shape = 1) +
scale_size_area(max_size=25) +
stat_smooth(method = lm)

The size argument in the ggplot call is passed down to both geom_point as well as stat_smooth. The problem is in the latter, where it is taken as the line width (I suppose). Moving the size into geom_point resolves the issue.
ggplot(data = x, aes(x = score, y = gdp.percapita)) +
geom_point(shape = 1, aes(size = gdp)) +
scale_size_area(max_size = 25) +
stat_smooth(method = lm) +
xlim(42, 84) +
xlab('economic index freedom') +
ylab('GDP per capita') +
theme_bw()

Related

How to change Y axis scaling limit in ggplot2

I am trying to draw a secondary axis plot of wind speed and wind direction.
p \<- ggplot(df, aes(x = date))
p \<- p + geom_line(aes(y = wd, colour = "wind direction"))
p \<- p + geom_line(aes(y = ws\*25, colour = "wind speed"))
p \<- p + scale_y_continuous(sec.axis = sec_axis(\~./25, name = "wind spped (ms )"))
p \<- p + scale_colour_manual(values = c("#e377c2", "#7f7f7f"))
p \<- p + labs(y = "wind direction (°)")
p \<- p + theme(text = element_text(size = 12)) + xlab("") + theme_light () +
theme(legend.position = "null") + theme(axis.text.x=element_blank())
p
I can able to produce the figure. However, I want to edit the scale of the primary axis as (0, 90 ,180, 270, 360). When I am adding ylim =c(0, 90, 180, 270, 360) in the primary axis, I the secondary axis is not showing up. Let me know how to slove this.
From the help for ylim:
Arguments ... For xlim() and ylim(): Two numeric values, specifying
the left/lower limit and the right/upper limit of the scale. If the
larger value is given first, the scale will be reversed. You can leave
one value as NA if you want to compute the corresponding limit from
the range of the data.
ylim, equivalent to scale_y_continuous(limits = ....) expects a vector with two elements, the min and max. You might want breaks instead if you are defining the y axis tick/grid/label levels.
set.seed(42)
df <- data.frame(date = as.Date("2023-01-01") + 30 * (0:3),
wd = 360*runif(4),
ws = 15*runif(4))
ggplot(df, aes(x = date, wd)) +
geom_line(aes(colour = "wind direction")) +
geom_line(aes(y = ws*25, colour = "wind speed")) +
scale_y_continuous(breaks = 90 * (0:4),
limits = c(0, 360),
sec.axis = sec_axis(~./25, name = "wind speed (ms )"))

How to plot two Y values for the same x with ggplot for a scatter plot

I am new to coding and R and wanted to try and use R for some data visualization. I am using ggplot and attempted to make a plot that has my species on the x axis, and on the y is the effect size calculations. I was successful in doing this. I have an effect size for size and one for muscle content of a mix of the species. I wanted to see if I could overlay the plot for size and the plot for muscle.
Image of Size plot
Image of Muscle plot
Here is my code:code image
Species <- SZ_M_spp_data$Species
y_m <- SZ_M_spp_data$`Mean(m)`
y_sz <- SZ_M_spp_data$`Mean(sz)`
y_both <- c(y_m,y_sz)
y_both
# ggplot2 plot
plot1 <- ggplot(SZ_M_spp_data, aes(Species, y_m)) +
geom_hline(yintercept = 0) +
geom_point(shape=19, size=3, aes(color = Species)) + #color
theme_classic () + # theme (white background)
xlab("Species") + #xlabels and
ylab("Mean Effect Size") + #y labels
ylim(-0.75,2.2) +
ggtitle("Maternal Size vs Egg Toxicant Load") + #title
theme(axis.text.x = element_text(angle = 45, hjust=1))
plot2 <- ggplot(SZ_M_spp_data, aes(Species, y_sz)) +
geom_hline(yintercept = 0) +
geom_point(shape=15, size=3, aes(color = Species)) + #color
theme_classic () + # theme (white background)
xlab("Species") + #xlabels and
ylab("Mean Effect Size") + #y labels
ylim(-0.75,2.2) +
ggtitle("Maternal Size vs Egg Toxicant Load") + #title
theme(axis.text.x = element_text(angle = 45, hjust=1))
I just wish to have the muscle and size points for the species on the same plot with different aes (sqaure for muscle and circles for size).. I just do not know how to have two different y values for the same x. Here is the raw data table
I look forward to your response, thanks!
You can create two geom_points. Specifying the same X axis in ggplot() function and the Y axis for each in the geom_point() function
Something like this
ggplot(data, aes(x = Specie)) +
geom_point(aes(y = variable1))+
geom_point(aes(y = variable2))

Visually unequal spaced log2 scaled axis with geom_smooth

I have a dataset containing descending HRs values (Y-axis) over Age (X-axis), stratified in two groups.
https://www.dropbox.com/s/l2p24llxcvndljl/reproducible_data_for_log2plot.txt?dl=0
I am trying to create two geom_smooth(method = "loess") with the Y-axis (HR) in log2 scale with uneven spacing, limited by specified breaks from 1 to 70.
Reading the data:
aLotOfHRs <- read.table("https://www.dropbox.com/s/l2p24llxcvndljl/reproducible_data_for_log2plot.txt?dl=1" , header = TRUE , sep = "\t")
My first attempt:
p <- ggplot(aLotOfHRs, aes(x = Age,y = HR,fill=Quantiles,color =Quantiles)) +
geom_smooth(method = "loess" , formula = y~x) +
# geom_point() +
theme_minimal() +
ylab("HR per 1-SD (log2 scale)") +
xlim(c(40,72)) +
scale_y_continuous(trans = scales::log2_trans())
1st Try
The scale is unfortunately visually equal but the log2 plot is correct. If I specify breaks, labels and limits:
p <- ggplot(aLotOfHRs, aes(x = Age,y = HR,fill=Quantiles,color =Quantiles)) +
geom_smooth(method = "loess" , formula = y~x) +
# geom_point() +
theme_minimal() +
ylab("HR per 1-SD (log2 scale)") +
xlim(c(40,72)) +
scale_y_continuous(trans = scales::log2_trans()
, breaks = log2(c(1.1,2,4,8,16,32,64))
, labels = c(1.1,2,4,8,16,32,64)
, limits = c(log2(1.1) , log2(70)))
# Note: I can't extend to 1 or it returns
# Error in seq.default(a, b, length.out = n + 1) :'from' must be a finite number
2nd Try
I obtain the right scale but the wrong plot.
The best result so far has been obtained transforming the coordinates last, but still I can't set the breaks the way I want them:
p <- ggplot(aLotOfHRs, aes(x = Age,y = HR,fill=Quantiles,color =Quantiles)) +
# geom_point() +
theme_minimal() +
ylab("HR per 1-SD of PRS (log2 scale)") +
xlim(c(40,70)) +
geom_smooth(method = "loess" , formula = y~x) +
ylim(c(1,70) ) +
coord_trans( y = "log2")
3d Try
Any suggestions?
This might be what you are after:
aLotOfHRs <- read.table("https://www.dropbox.com/s/l2p24llxcvndljl/reproducible_data_for_log2plot.txt?dl=1" , header = TRUE , sep = "\t")
p <- ggplot(aLotOfHRs, aes(x = Age,y = HR,fill=Quantiles,color =Quantiles)) +
# geom_point() +
theme_minimal() +
ylab("HR per 1-SD of PRS (log2 scale)") +
xlim(c(40,70)) +
geom_smooth(method = "loess" , formula = y~x)
p + scale_y_continuous(trans = scales::log2_trans(),breaks = c(1,2,4,8,16,32,64), lim = c(1,100), expand = c(0,0))
This is your 3rd one, but with the added scale_y_continuous() call at the end.

Add custom, multiple geom_vline to each faceted ggplot2

I have a dataset where each species was mixed with a certain density (numeric) and type (numeric) of another species. I want to add two types of vertical lines to each of my facet_grid panels in ggplot: (a) A fixed line which dives the density/ type. e.g. 1000/1 = 1000, 1000/6 = 166.7, 10000/1 = 10000, 10000/6 = 1666.7
set.seed(111)
count <- rbinom(500,100,0.1)
species <- rep(c("A","B"),time = 250)
density <- rep(c("1000","10000","1000","10000"),time = 125)
type <- rep(c("1","1","6","6"),time = 125)
df <- data.frame(species, density, type, count) # I feel too naiive, but I'm not able to get all the treatments filled. Gah.
ggplot(df, aes(x= count, colour = species, fill = species)) +
geom_histogram(position="identity", alpha=0.5) +
theme_bw() + ylab("Frequency") +
facet_grid(species ~ type + density) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
theme(legend.position = "none") + theme(aspect.ratio = 1.75/1)

Plotting multiple x axis profiles from a csv file in R?

I am planning to plot vertical profile of multiple parameters on x axis, for example, salinity, temperature, density, against pressure as y axis, in the same graph. This is the kind of plot i am hoping to get :
Here is a sample from my data :
ï..IntD.Date. IntT.Time. Salinity..psu. SIGMA.Kg.m3. Pressure.dbar.
1 21-April-2019 5:31:55 PM 30.2502 20.2241 0.7160
2 21-April-2019 5:32:00 PM 31.0254 20.8081 0.8409
3 21-April-2019 5:32:05 PM 31.2654 20.9930 1.0551
4 21-April-2019 5:32:10 PM 31.2953 21.0176 1.2694
Temp..0C. Vbatt.volt.
1 23.4054 12.29
2 23.4148 12.30
3 23.4060 12.29
4 23.4024 12.33
I already used these codes:
data <- read.csv('file location')
vert_plot <- ggplot(data, aes(x = Pressure.dbar., y = Temp..0C.)) + geom_line(color = '#088DA5', size = 0.75) + labs(size = 18) + ggtitle("temp vs pressure") + theme_grey() + coord_flip() + scale_y_reverse()
Which generated this plot :
as you can see, i was able to bring a single profile where the scale of y axis wasn't in reverse order whereas I'd prefer pressure value (0, 5, 10....) starting from the top left corner. Unlike the plot i made where pressure value begins in bottom left corner.
I'd be grateful if someone helped me to get figure where i will be able to plot multiple vertical profile in same graph where y axis is pressure and is in reverse order, as shown in that barrier layer thickness picture.
Add as many geom_line() as required and call aes in each geom_line(). For breaks of 5, add scale_x_continuous and call sequence of breaks in it.
vert_plot <- ggplot(df) +
geom_line(aes(x = Pressure.dbar., y = Temp..0C.), color = 'blue', size = 0.75) +
geom_line(aes(x = Pressure.dbar., y = Salinity..psu.), color = 'red', size = 0.75) +
geom_line(aes(x = Pressure.dbar., y = SIGMA.Kg.m3.), color = 'green', size = 0.75) +
labs(size = 18) + ggtitle("Dummy Title") + xlab("Pressure") + ylab("Dummy Label") +
scale_x_reverse(limits = c(40, 0), breaks = seq(40, 0, -5)) +
theme_grey() + coord_flip() + scale_y_reverse()
Alternate method:
Instead of going through all these, you can melt the data frame keeping the variable names as groups.
library(reshape2)
newdf <- melt(df, id.vars = c("IntD.Date.", "IntT.Time.", "Pressure.dbar."),
variable.name = "group")
vert_plot <- ggplot(newdf, aes(x = Pressure.dbar., y = value, color = group)) +
geom_line(size = 0.75) +
labs(size = 18) + ggtitle("Dummy Title") +
xlab("Pressure") + ylab("Dummy Label") +
scale_x_reverse(limits = c(40, 0), breaks = seq(40, 0, -5)) +
theme_grey() + coord_flip() + scale_y_reverse()

Resources