本文介绍了自定义ggplot2轴和标签格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制看起来信息丰富,清晰整洁的标签.

I'm trying to draw labels which look informative, clear and tidy.

遵循以下示例并提出了一个有关labelaxis格式的更多问题.

I was following example and raised one more question about label and axis formatting.

例如,我有一些销售数据,其中包括欧元的品牌,类别和支出.当EUR的金额很大(数百万或更多)时,标签看起来真的很难阅读且没有信息量.

For example, I have sales data which includes Brand, Categories and Expenditure in EUR. When sum of EUR is big (millions or more) labels look really hard to read and not informative.

结果,x-axisScientific notation中,并且看上去也很不干净.

As a result, x-axis is in Scientific notation and also looks really uncleanly.

我已经设法以自定义方式设置标签格式:它以千为单位显示Eur. geom_text(aes(label= paste(round(EUR/1000,0),"€"), y=pos), colour="white")有没有更简单或自动化的方法?

I've manage to format labels in custom way: it shows Eur in thousands. geom_text(aes(label= paste(round(EUR/1000,0),"€"), y=pos), colour="white")Is there an easier or automated way?

由于Scientific notation看起来真的不清楚,对于我尝试使用scale_y_continuous(formatter = "dollar")的轴,但这似乎不起作用.而且,我无法找到是否还实施了Eur而不是美元.我相信最好在thousands中显示y-axis.有解决方案吗?

As Scientific notation looks really unclear, for axis I tried using scale_y_continuous(formatter = "dollar"), but this seems do not work. Moreover, I was unable to find if there is Eur also implemented instead of dollar. I believe that it would be the best to show y-axis in thousands.Any solutions?

此外,我附上可复制的示例:

Also, I enclose reproducible example:

library(plyr)
library(dplyr)
library(ggplot2)
library(scales)


set.seed(1992)
n=68

Category <- sample(c("Black", "Red", "Blue", "Cyna", "Purple"), n, replace = TRUE, prob = NULL)
Brand <- sample("Brand", n, replace = TRUE, prob = NULL)
Brand <- paste0(Brand, sample(1:5, n, replace = TRUE, prob = NULL))
EUR <- abs(rnorm(n))*100000

df <- data.frame(Category, Brand, EUR)


df.summary = df %>% group_by(Brand, Category) %>%
  summarise(EUR = sum(EUR)) %>%   # Within each Brand, sum all values in each Category
  mutate( pos = cumsum(EUR)-0.5*EUR)



ggplot(df.summary, aes(x=reorder(Brand,EUR,function(x)+sum(x)), y=EUR, fill=Category)) +
  geom_bar(stat='identity',  width = .7, colour="black", lwd=0.1) +
  geom_text(aes(label=ifelse(EUR>100,paste(round(EUR/1000,0),"€"),""),
                y=pos), colour="white") +
  coord_flip()+
  labs(y="", x="")

推荐答案

您可以在dollar_format中为欧元而不是美元设置前缀:

You can set the prefix in dollar_format for euros instead of dollars:

scale_y_continuous(labels=dollar_format(prefix="€")) +

这可以解决科学计数法问题.

That takes care of the scientific notation issue.

要获得成千上万的内容,您可以在创建摘要时除以1000.为了减少混乱,您可以在条形标签中省略欧元符号,但是我将符号保留在以下示例中:

To get everything in thousands, you could just divide by 1000 when you create the summary. To reduce clutter, you could leave out the euro symbol in the bar labels, but I've kept the symbol in the example below:

df.summary = df %>% group_by(Brand, Category) %>%
  summarise(EUR = sum(EUR)/1000) %>%   # Within each Brand, sum all values in each Category
  mutate( pos = (cumsum(EUR)-0.5*EUR))

ggplot(df.summary, aes(x=reorder(Brand,EUR,function(x)+sum(x)), y=EUR, fill=Category)) +
  geom_bar(stat='identity',  width = .7, colour="black", lwd=0.1) +
  geom_text(aes(label=ifelse(EUR>100,paste0("€", round(EUR,0)),""),
                y=pos), colour="white") +
  scale_y_continuous(labels=dollar_format(prefix="€")) +
  coord_flip()+
  labs(y="Thousands of €", x="")

这篇关于自定义ggplot2轴和标签格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 19:35