Plot with multiple y-axes in Julia with PyPlot backend - matplotlib

I'm new to Julia and I'm trying to make plot using PyPlot back-end with multiple Y-Axes, more than two.
Using:
twinx()
I'm able to plot two y-axes, and plot multiple line on each y-axis. But as soon as I try to use more than two y-axes all the y-axes on the right are superposed. Is there a way to add a margin between the axes?
Below a simple example:
using LaTeXStrings
using Plots
pyplot()
# Sets the default font family for the plots.
plot_font = "CMU Typewriter Text"
a = [1; 2; 3]
b = [3; 5; 2]
c = [4; 7; 1]
d = [7; 2; 12]
e = [3; 9; 1]
plSz = (1600, 900)
lbl = "line 1"
title = "Test"
xlabel = "xlabel"
ylabelx = "ylabel 1"
gfc0 = Plots.PyPlot.figure(title, figsize = (10, 10))
gfc0 = plot(
a,
b,
label = lbl,
size = plSz,
marker = :xcross,
legend = :topleft,
fontfamily = plot_font,
linewidth = 2,
grid = true,
minorgrid = true,
minorgridls = :dot,
gridls = :dash,
gridlw = 1,
minorgridlw = 0.75,
background_color = RGB(7/255,54/255,66/255),
right_margin = 25Plots.mm
)
gfc0 = title!(title)
gfc0 = xlabel!(xlabel)
gfc0 = ylabel!(ylabelx)
gfc0 = xaxis!(minorticks=10)
gfc0 = yaxis!(minorticks=10)
lbl = "line 2"
ylabelx = "ylabel 2"
gfc1 = twinx(gfc0)
plot!(
gfc1,
a,
c,
label = lbl,
marker = :cross,
legend = :topright,
linewidth = 2,
ylabel = ylabelx,
linestyle = :dash
)
lbl = "line 3"
ylabelx = "ylabel 3"
gfc2 = twinx(gfc1)
plot!(
gfc2,
a,
d,
label = lbl,
legend = :top,
marker = :circle,
linewidth = 2,
ylabel = ylabelx,
linestyle = :dot
)
lbl = "line 4"
plot!(
gfc2,
a,
d,
label = lbl,
linewidth = 2,
marker = :circle,
linestyle = :dot
)
display(gfc0)
The result of this code looks like this:
Plot with 3 y-axes and the two on the right are superposed

Related

Why are Y axis labels offset in ggplot?

The Y axis labels are offset
The Y axis labels from 100 to the top are aligned to the right, while from 90 to the bottom are aligned to the left. I've looked at many paramaters and I couldn't find one to be causing this. Also, I haven't found anyone else with this same issue.
Here's my code:
test <- ggplot(data,
aes(x=Month, y=Value, color=Name, group=Name, fill=Name))+
geom_line(size=3)+
geom_point(size=5)+
scale_color_manual(values=c("#FBE785","#0F5F00","#FFC300","#1BFFAA"))+
ylab ("")+
xlab ("")+
labs(caption = paste("Fonte: Fred e IBGE.")) +
scale_x_date(date_labels = "%b/%y", breaks = "6 month", expand=c(0,0))+
coord_cartesian(clip = "off")+
theme_minimal() +
guides(fill=guide_legend())+
theme(panel.background = element_rect(fill= "#122929",color = "#122929"),
plot.background = element_rect(fill = "#122929"),
panel.grid.major = element_line(color = "#4D4B55", size =0.1),
panel.grid.minor = element_line(color= "#4D4B55", size =0.1),
panel.grid = element_blank(),
axis.text.y = element_text(vjust = 1, hjust=-1),
axis.text.x = element_text(vjust = -1, hjust=0),
legend.title = element_blank(),
legend.position = "bottom",
legend.key.width = unit(1.5, "cm"),
plot.caption = element_text(family = "Abel",vjust = -1, hjust = 0,colour="#4D4B55", size= 30),
text = element_text(family = "Abel", color = "#4D4B55",size = 35),
plot.margin = margin(1,1,1.5,1.2, "cm"))
ggsave("./test.png", width = 21, height = 15, dpi = 300)
PS: Not sharing the data itself because I guess that's not where the problem is.
Thanks!
Your text is misaligned because of your axis.text.y argument. Change hjust to 1 and it will be properly aligned. I have provided a minimal reproducible example below.
library(tidyverse)
Month <- rep(x = month.abb, times = 10)
Value <- sample(x = 10:120, size = 120, replace = TRUE)
Name <- sample(x = LETTERS[1:4], size = 120, replace = TRUE)
data <- data.frame(Month, Value, Name)
ggplot(data, aes(x = Month, y = Value, color = Name, group = Name, fill = Name)) +
geom_line(size = 1) + geom_point(size = 2) +
theme(axis.text.y = element_text(vjust = 1, hjust = 1)) # <- problem here

face='bold' of axis.text do not work and axis.line only control left and bottom in ggplot

library(terra)
library(raster)
library(rasterVis)
f <- system.file("external/test.grd", package="raster")
r <- rast(f)
r
rr <- project(x=r,y='epsg:4326')
rr
gplot(rr)+
geom_raster(aes(fill = value),na.rm=T)+
scale_fill_distiller(na.value = NA)+
ggtitle(label = 'Test')+
coord_sf(crs=4326)+
theme(axis.title = element_blank(),
axis.line = element_line(colour='black', linewidth = 3,
linetype=1),
axis.line.x.top = element_line(colour='black', linewidth = 1,
linetype=1),
axis.line.y.right = element_line(colour='black', linewidth = 1,
linetype=1),
plot.title = element_text(hjust = 0.5,size = 20,
face = 'bold',family = 'serif'),
axis.text = element_text(size=20,face='bold',
family = 'serif',color = 'black'),
legend.title=element_text(face='bold',family = 'serif',
size = 15),
legend.text = element_text(size = 15,
face='bold',
family='serif'),
panel.background = element_rect(fill=NA,color = 'black')
)
I met this situation. I checked these codes many times, but the problem seemed still occured.
And I have tried tidyterra package as well. The same problem.
Ref:https://github.com/oscarperpinan/rastervis/issues/96

Add linetype to geom_segment legend

Aim: add the linetypes of the segments to the legend, as well as the colour.
Problem: only the colour is showing.
Data:
m = as.data.frame(matrix(c(1:10), ncol = 2, nrow = 10))
Plot:
ggplot(m, aes(v1,v2)) + geom_segment(aes(x = 0, xend = 9.75, y = 10, yend = 10, colour = "PEL"), linetype = "dotted") + geom_segment(aes(x = 0, xend = 9.75, y = 5, yend = 5, colour = "AL1"), linetype = "longdash") + geom_segment(aes(x = 0, xend = 9.75, y = 2, yend = 2, colour = "ISQG"), linetype = "solid") + scale_colour_manual("legend", values = c("PEL" = "black", "AL1" = "blue", "ISQG" = "purple"), guide = guide_legend(override.aes = list(alpha = 1))) + theme(legend.position = "bottom")
I've tried adding scale_linetype_manual(values = c("PEL" = "dotted", "AL1" = "longdash", "ISQG" = "solid") but nothing changes.
This answer is similar, Legend linetype in ggplot but I couldn't figure out how to make it work with geom_segment
Thank you in advance
The most ggplot-esque way of doing this, is to include a linetype variable as part of mapping in the aes() functions. You must then ensure that both the linetype and colour scales have the same titles, breaks, limits, labels etc.
Alternatively, you can also include the linetype in the override.aes part of guide_legend().
library(ggplot2)
ggplot() +
geom_segment(
aes(x = 0, xend = 9.75, y = 10, yend = 10, colour = "PEL", linetype ="PEL"),
) +
geom_segment(
aes(x = 0, xend = 9.75, y = 5, yend = 5, colour = "AL1", linetype ="AL1"),
) +
geom_segment(
aes(x = 0, xend = 9.75, y = 2, yend = 2, colour = "ISQG", linetype = "ISQG"),
) +
scale_colour_manual(
"legend",
values = c("PEL" = "black", "AL1" = "blue", "ISQG" = "purple"),
) +
scale_linetype_manual(
"legend",
values = c("PEL" = "dotted", "AL1" = "longdash", "ISQG" = "solid"),
) +
theme(legend.position = "bottom")
Created on 2022-05-19 by the reprex package (v2.0.1)

Wrong coloring in ggplot line graphs

I have created a ggplot graph with three lines. Each line represents a different column in a data frame and colored in a different color. For some reason, the colors in the final graph are not coordinated to the code.
The data frame:
Scenario 1 Scenario 2 Scenario 3 Years
0.0260 0.0340 0.0366 1
0.0424 0.0562 0.0696 2
0.0638 0.0878 0.1150 3
0.0848 0.1280 0.1578 4
0.1096 0.1680 0.2074 5
0.1336 0.2106 0.2568 6
This is the code:
ggplot(ext2, aes(x = Years))+
geom_line(aes(y = `Scenario 1`, color = "darkblue"))+
geom_line(aes(y = `Scenario 2`, color = "darkred"))+
geom_line(aes(y = `Scenario 3`, color = "darkgreen"))+
xlab("Years")+
ylab("Quasi - extinction probability")+
ggtitle("2 mature individuals")+
geom_segment(aes(x = 45,y = 0.5, xend = 45, yend = 1.1),linetype = "longdash")+
geom_segment(aes(x = 75,y = 0.2, xend = 75, yend = 0.5),linetype = "longdash")+
geom_segment(aes(x = 0,y = 0.5, xend = 100, yend = 0.5),linetype = "longdash")+
geom_segment(aes(x = 0,y = 0.2, xend = 100, yend = 0.2),linetype = "longdash")+
geom_text(x = 20, y = 0.80, label = "CE")+
geom_text(x = 40, y = 0.35, label = "EN")+
scale_colour_manual(values = c("darkblue", "darkred","darkgreen"), labels = c("Scenario 1","Scenario 2","Scenario 3"))+
theme(legend.title = element_blank())+
theme_minimal()
and this is the graph:
Click here to see graph
The problem is that what I defined as 'scenario 3' in the code is actually a representation of 'scenario 2' in the data frame. You can see it according to the values under scenario 2 in the data frame.
For ggplot, the data needs to be in long format before you plot. Then, you can make "Scenarios" (i.e., name) the group, so that you can manually color the individual lines (i.e., with scale_colour_manual).
library(tidyverse)
ext_long <- ext2 %>%
pivot_longer(!Years)
ggplot(ext_long, aes(x = Years, color = name)) +
geom_line(aes(y = value)) +
xlab("Years") +
ylab("Quasi - extinction probability") +
ggtitle("2 mature individuals") +
geom_segment(aes(
x = 45,
y = 0.5,
xend = 45,
yend = 1.1
), linetype = "longdash") +
geom_segment(aes(
x = 75,
y = 0.2,
xend = 75,
yend = 0.5
), linetype = "longdash") +
geom_segment(aes(
x = 0,
y = 0.5,
xend = 100,
yend = 0.5
), linetype = "longdash") +
geom_segment(aes(
x = 0,
y = 0.2,
xend = 100,
yend = 0.2
), linetype = "longdash") +
geom_text(x = 20, y = 0.80, label = "CE") +
geom_text(x = 40, y = 0.35, label = "EN") +
scale_colour_manual(
values = c("darkblue", "darkred", "darkgreen"),
labels = c("Scenario 1", "Scenario 2", "Scenario 3")
) +
theme(legend.title = element_blank()) +
theme_minimal()
Output (only have a small part of the data, which is the reason the lines do not extend across the graph)
Data
ext2 <- structure(
list(
Scenario.1 = c(0.026, 0.0424, 0.0638, 0.0848,
0.1096, 0.1336),
Scenario.2 = c(0.034, 0.0562, 0.0878, 0.128,
0.168, 0.2106),
Scenario.3 = c(0.0366, 0.0696, 0.115, 0.1578,
0.2074, 0.2568),
Years = 1:6
),
class = "data.frame",
row.names = c(NA,-6L)
)

How to label line plot in a combined bar & line plot in ggplot r?

I want to combine a bar and line plot and label line plot.
This is what I got: plot
this is my code:
df %>%
ggplot(aes(reorder(NAME, pval),y = pval)) +
geom_col(aes(x = NAME, y = pval), size = 1, color = "royalblue", fill = "white") +
geom_line(aes(x = NAME, y = 10*Ratio), size = 1.5, color="#c4271b", group = 1) + geom_text(aes(label = Ratio))+coord_flip()
I want to label line plot, but the bar plot gets the labels?
My second question:
How to rearrange the y-axis from the largest -log(pvalue) to lowest one?
Any help will be really appreciated!
try set the x and y aes in geom_text() with the same in geom_line()
geom_text(aes(x = NAME, y = 10*Ratio, label = Ratio))