I have the following data:
data= structure(list(inv_kT = c(42.6123919742007, 42.4799809266797,
42.4722825012597, 42.4510843820046, 42.4193914112338, 42.4122074755519,
42.3764231025678, 42.3067291931282, 42.1980484435468, 42.1420505628077,
42.0840522587684, 42.0218386653298, 41.9609504123289, 41.8854603414116,
41.8158696171797, 41.8158696171797, 41.7619829798769, 41.702520428422,
41.6554420516226, 41.6383703319267, 41.6197908437627, 41.6197908437627,
41.5936651167378, 41.5145777106756, 41.4985915801753, 41.4453949718182,
41.4453949718182, 41.4591160881044, 41.271072066776, 41.271072066776,
41.0120282074906, 40.8657100886557, 41.0774968826629, 41.0774968826629,
41.0025400642, 40.9028738414799, 40.9028738414799, 41.007896375581,
41.0083442824568, 41.0083442824568, 41.0390268480029, 41.0390268480029,
41.0130046153368, 41.1646411216982, 41.1646411216982, 41.1797646554088,
41.1797646554088, 41.1956860463927, 41.2194706851824, 41.2194706851824
), ln_SR = c(4.91998092582813, 4.63472898822964, 4.77068462446567,
4.56434819146784, 4.77068462446567, 4.82028156560504, 4.80402104473326,
4.61512051684126, 4.56434819146784, 4.40671924726425, 4.35670882668959,
4.27666611901606, 4.15888308335967, 4.11087386417331, 4.11087386417331,
4.12713438504509, 4.02535169073515, 3.98898404656427, 3.93182563272433,
3.73766961828337, 3.66356164612965, 3.58351893845611, 3.58351893845611,
3.85014760171006, 3.85014760171006, 4.29045944114839, 4.24849524204936,
4.17438726989564, 4.29045944114839, 4.38202663467388, 5.19849703126583,
5.37063802812766, 4.89034912822175, 4.74493212836325, 4.69134788222914,
4.80402104473326, 4.62497281328427, 4.30406509320417, 4.26267987704132,
4.23410650459726, 4.29045944114839, 3.89182029811063, 3.68887945411394,
3.63758615972639, 3.46573590279973, 3.40119738166216, 3.40119738166216,
3.3322045101752, 3.3322045101752, 3.13549421592915)), row.names = c(NA,
-50L), class = c("tbl_df", "tbl", "data.frame"))
I try to plot it along with abline and 2nd x -axis:
library(ggplot2)
library(scales)
k <- 8.617333262145e-05 # Boltzmann constant in eV/K
ggplot(data, aes(x = inv_kT, y = ln_SR)) +
geom_point(color="black") +
geom_abline(slope = -0.70, intercept=32.72)
when I try to add the second axis and reverse scale:
ggplot(data, aes(x = inv_kT, y = ln_SR)) +
geom_point(color="black") +
geom_abline(slope = -0.70, intercept=32.72)+
scale_x_continuous(
trans="reverse",
sec.axis=sec_axis(trans=~ (1/.)/k-273.15,
name="Temperature (°C)",
breaks=pretty_breaks(n=5)))
abline line disappears, i don't understand why.
Why does it not move together with the axis? I know if I replace the slope with positive number, the abline appears, but do I need to modify the intercept too??
It is a known issue that geom_abline
doesn't transform when your are reversing your axis. You don't need to modify the intercept because your are only reversing your x-axis and not your y-axis. You could slightly change the slope to get the desired result like this:
library(ggplot2)
library(scales)
k <- 8.617333262145e-05 # Boltzmann constant in eV/K
ggplot(data, aes(x = inv_kT, y = ln_SR)) +
geom_point(color="black") +
geom_abline(slope = abs(-0.70), intercept=32.72) +
scale_x_continuous(
trans="reverse",
sec.axis=sec_axis(trans=~ (1/.)/k-273.15,
name="Temperature (°C)",
breaks=pretty_breaks(n=5)))
Created on 2023-03-29 with reprex v2.0.2