How to use TRANS_NEW in GGPLOT2 to move proportional to proportion in GGPLOT2 without muting

Suppose I have a continuous x-axis, I want to add a constant value to each tickmark tag. For example,

 ggplot(data=warpbreaks,aes(x=breaks,y=replicate)) + geom_point()

… There will be markers at 20, 40 and 60. If I want these markers to not change the rest of the graph What should I do if it reads 40, 60 and 80? I tried this:

ggplot(data=warpbreaks,aes(x=breaks,y=replicate)) + geom_point() + 
scale_x_continuous(trans=trans_new ("shift",function(x) x+20,identity))

This moves the mark by the appropriate amount, but also deletes the leftmost mark. If I use x 40 instead, then from The two tick marks are omitted on the left and only one is left. If I do x 10, then all of them are shifted by that amount, and none of them are omitted. What’s the matter? How to move the x value reliably?

Motivation: When I fit a regression model, I usually center the numeric predictor variables (unless they actually contain zero) to avoid absurd/misleading main effects estimates that are outside the range supported by the data. However, when I plot the data and/or fitted values, I want to return them to the original non-central scale.

The first option is proposed by @mnel:

ggplot(data=warpbreaks,aes(x=breaks,y=wool)) + geom_point() + 
scale_x_continuous(labels=function(x) x+20)

Please note that you can change the data “locally” (within the ggplot call), so this is another option :

ggplot(data=warpbreaks,aes(x=breaks+20,y=wool)) + geom_point()

In this case , No need to patch the scale.

Assuming I have a continuous x-axis, I want to add a constant value to each tickmark tag. For example,

ggplot(data=warpbreaks,aes(x=breaks,y=replicate)) + geom_point()

…There will be marks at 20, 40 and 60. What if I want these markers to read 40, 60, and 80 without changing the rest of the graphs? I tried this:

ggplot(data=warpbreaks,aes(x=breaks,y=replicate)) + geom_point() + 
scale_x_continuous(trans=trans_new ("shift",function(x) x+20,identity))

This moves the mark by the appropriate amount, but also deletes the leftmost mark. If I use x 40 instead, then from The two tick marks are omitted on the left and only one is left. If I do x 10, then all of them are shifted by that amount, and none of them are omitted. What’s the matter? How to move the x value reliably?

Motivation: When I fit a regression model, I usually center the numeric predictor variables (unless they actually contain zero) to avoid absurd/misleading main effects estimates that are outside the range supported by the data. However, when I plot the data and/or fitted values, I want to return them to the original non-central scale.

The first option is defined by @ mnel proposed:

ggplot(data=warpbreaks,aes(x=breaks,y=wool)) + geom_point() + 
scale_x_continuous(labels= function(x) x+20)

Please note that you can change the data “locally” (within the ggplot call), so this is another option:

ggplot(data=warpbreaks,aes(x=breaks+20,y=wool)) + geom_point()

In this case, there is no need to repair the scale.

Leave a Comment

Your email address will not be published.