I need help in removing/converting the negative values in the x-axis of the pyramid plot. I was able to build the pyramid through https://walker-data.com/census-r/exploring-us-census-data-with-visualization.html subheading 4.5.2. I keep getting an error saying function not found: number_format.
I think the scale_x_continuous is where I'd be able to change the - sign but returns an error everytime
So from the context of the plot, I am assuming that you don't want to convert the values themselves, but rather convert the x-axis so that the male and female sides of the pyramid are mirrored?
In which case, you can use abs
. The function abs(x)
computes the absolute value of x, which would remove the negative sign from your data's values.
Without seeing a reproducible example of your code (which should be included when you ask questions like this on Stack Overflow, see the package {reprex} for help with this), it's a little difficult to be sure exactly what you need to change to make the code work, but I think you should be on the right track with scale_x_continuous
.
With regard to the error that you're receiving, it suggests that you haven't imported the library for that function, {scales}, as suggested by stefan in the comments (and as suggested, scales:::label_number
has superseded scales:::number_format
, so you should use the former).
If you're using the scale_x_continuous
code from the second plot in Section 4.5.2 of the link you have shared:
utah_pyramid +
scale_x_continuous(
labels = ~ number_format(scale = .001, suffix = "k")(abs(.x)),
limits = 140000 * c(-1, 1)
)
The number_format
function isn't the part of the code that is producing the absolute values, it is converting the scale of the values to thousands. It is the abs(.x)
part that is removing the negative sign.