\n
Here's a minimal working example using mtcars
- despite specifying the upper x-limit to be 10, the plot shows data past that range. I'd like the plot to cropped to the limits specified. Is there a way to do this?
ggplot(as.data.table(mtcars)) + \n geom_line(aes(x = wt, y = mpg, color= factor(cyl))) + \n ylab('Fuel Economy (mpg)') + \n scale_y_continuous(sec.axis = sec_axis(~.*1.6/3.7854, name = 'Fuel Economy (kmpl)')) + \n xlab('Weight (lbs)') + \n scale_x_continuous(sec.axis = sec_axis(~./2.20462, name = 'Weight (kg)'), position = 'bottom') + \n theme_light() + \n theme(\n legend.position = c(0.15, 0.75),\n legend.title = element_blank(),\n axis.title.y.right = element_text(\n angle = 90,\n margin = margin(r = 0.8 * 11,\n l = 0.8 * 11 / 2)\n )\n ) + \n coord_cartesian(xlim = c(0, 5), ylim = c(10, 35))\n
\n\n","author":{"@type":"Person","name":"Gautam"},"upvoteCount":0,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Add limits and expand arguments in scale_x_continuous and scale_y_continuous. You can add breaks as well.
\nggplot(as.data.table(mtcars)) + \n geom_line(aes(x = wt, y = mpg, color= factor(cyl))) + \n ylab('Fuel Economy (mpg)') + \n scale_y_continuous(limits = c(10, 35), expand = c(0, 0), \n sec.axis = sec_axis(~.*1.6/3.7854, name = 'Fuel Economy (kmpl)')\n ) + \n xlab('Weight (lbs)') + \n scale_x_continuous(limits = c(0, 5), expand = c(0, 0), \n sec.axis = sec_axis(~./2.20462, name = 'Weight (kg)'), position = 'bottom') + \n theme_light() + \n theme(\n legend.position = c(0.15, 0.75),\n legend.title = element_blank(),\n axis.title.y.right = element_text(\n angle = 90,\n margin = margin(r = 0.8 * 11,\n l = 0.8 * 11 / 2)\n )\n ) \n
\n\n","author":{"@type":"Person","name":"Mohanasundaram"},"upvoteCount":1}}}Reputation: 2753
I'm using secondary axes (scaled) to show metric/imperial units for a plot. I'd like to have the plot area to be limited to (1500,0) and (5000,350) - both sets in principal coordinates. I've tried the following:
xlim(c(1500,5000)) + ylim(c(0,350))
lims
with the same arguments as abovecoord_cartesian(xlim = c(1500, 5000), ylim = c(0, 350))
None of these made any change to my plot. I'd like to get rid of the areas highlighted by the red lines (crop the plot):
Here's a minimal working example using mtcars
- despite specifying the upper x-limit to be 10, the plot shows data past that range. I'd like the plot to cropped to the limits specified. Is there a way to do this?
ggplot(as.data.table(mtcars)) +
geom_line(aes(x = wt, y = mpg, color= factor(cyl))) +
ylab('Fuel Economy (mpg)') +
scale_y_continuous(sec.axis = sec_axis(~.*1.6/3.7854, name = 'Fuel Economy (kmpl)')) +
xlab('Weight (lbs)') +
scale_x_continuous(sec.axis = sec_axis(~./2.20462, name = 'Weight (kg)'), position = 'bottom') +
theme_light() +
theme(
legend.position = c(0.15, 0.75),
legend.title = element_blank(),
axis.title.y.right = element_text(
angle = 90,
margin = margin(r = 0.8 * 11,
l = 0.8 * 11 / 2)
)
) +
coord_cartesian(xlim = c(0, 5), ylim = c(10, 35))
Upvotes: 0
Views: 950
Reputation: 2949
Add limits and expand arguments in scale_x_continuous and scale_y_continuous. You can add breaks as well.
ggplot(as.data.table(mtcars)) +
geom_line(aes(x = wt, y = mpg, color= factor(cyl))) +
ylab('Fuel Economy (mpg)') +
scale_y_continuous(limits = c(10, 35), expand = c(0, 0),
sec.axis = sec_axis(~.*1.6/3.7854, name = 'Fuel Economy (kmpl)')
) +
xlab('Weight (lbs)') +
scale_x_continuous(limits = c(0, 5), expand = c(0, 0),
sec.axis = sec_axis(~./2.20462, name = 'Weight (kg)'), position = 'bottom') +
theme_light() +
theme(
legend.position = c(0.15, 0.75),
legend.title = element_blank(),
axis.title.y.right = element_text(
angle = 90,
margin = margin(r = 0.8 * 11,
l = 0.8 * 11 / 2)
)
)
Upvotes: 1