英文:
Customize logtransformed labels in ggplot
问题
Sure, here is the translated content:
嗨,
我创建了一个具有不同类别的条形图。所有类别都以相同的比例来衡量,但每个类别的值在不同级别上。最好将y轴比例转换为log10,以显示所有类别。
使用以下代码非常简单:
scale_y_continuous(trans = log10_trans(),
breaks = trans_breaks("log10", function(x) 10^x),
labels = label_number_auto())
现在我想自定义标签。因为我展示了不同类别的支出,所以将y轴标记为"0.001百万欧元","0.01百万欧元","0.1百万欧元","1百万欧元","10百万欧元"等,而不是1000,10000,100000,1000000,10000000等。
我该如何更改标签?
#labels = paste(label_number_auto()/1000000,"百万欧元")
不起作用。
感谢任何提示。
如果需要,我可以创建一个示例
希望这对你有帮助。
英文:
Hei,
I created a barplot with different categories. All categories a meassured on the same scale but the value of each catecory is on different levels. Best is to transform the y scale by log10 to show all categories.
This is very easy with this code:
scale_y_continuous(trans = log10_trans(),
breaks = trans_breaks("log10", function(x) 10^x),
labels = label_number_auto())
Now I want to customize the labels. Because I show spendings for different categories, it would be fine to label the y axis with "0.001 Mio€","0.01 Mio€","0.1 Mio€", "1 Mio€", "10 Mio€" and so on instead of 1000,10000,100000,1000000,10000000 and so on.
How can i change the labels?
#labels = paste(label_number_auto()/1000000,"Mio€")
does not work.
Thank you for any hint.
I create an example if needed
V
答案1
得分: 1
使用一个格式化函数来处理标签,并将它们手动转换为对数尺度:
ggplot(diamonds, aes(color, log10(price))) +
geom_boxplot() +
scale_y_continuous(labels = function(x) paste(round(10^x)/1000, "Tsd. €"))
在ggplot 3.2.0版本中进行了测试。
可以通过不进行修改的方式来查看两个尺度是相同的:
ggplot(diamonds, aes(color, price)) +
geom_boxplot()
英文:
Use a formatter function for the labels and transform them to a log scale "by hand":
ggplot(diamonds, aes(color, log10(price))) +
geom_boxplot() +
scale_y_continuous(labels = function(x) paste(round(10^x)/1000, "Tsd. €"))
Tested with ggplot 3.2.0.
The fact that the two scales are identical can be seen by plotting without modifications:
ggplot(diamonds, aes(color, price)) +
geom_boxplot()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论