溢出X轴(ggplot2) [英] Overflowing X axis (ggplot2)
问题描述
我有一份包含三个变量的公司名单:创建年份,市值和公司名称(实际上是股票代码)。
我想通过创建年份(X轴)和市值(Y轴)来绘制它们,填充由供应商名称确定。
完成此操作:
qplot(factor(Established),Market .Cap,data = mcap,geom =bar,fill = Vendor)
但X轴难以辨认(见下文)。我试图提供scale_x_discrete()值(1900 - 2012年等),但没有运气。如何告诉ggplot2不要显示实际值,而是显示我选择的范围?
另外,如果您能提醒我如何避免Y轴上的科学记数法,那就太棒了。
我希望这有助于。如果旋转轴不起作用,则可以使用间隔
和标签
在 scale_x_continuous
中,如下所示:
require(ggplot2)
#虚拟数据
set.seed(45)
len < - 50
df < - data.frame(年=因子(seq(1901,1950,length.out = len )),
values = 1e6 * runif(len),group = factor(rep(1:5,each = len / 5)))
p < - ggplot(data = df,aes(x =年,fill = group))+ geom_bar(aes(weight = values))
require(scales)#去除科学记数法
p < - p + scale_y_continuous(labels = comma)
#手动生成中断/标签
标签< - seq(1901,2000,length.out = 10)
#并设置中断和标签
p < - p + scale_x_discrete(breaks = labels ,labels = as.character(labels))
p
I have a list of companies that includes three variables: year founded, market capitalization and company name (actually, ticker symbol).
I want to plot them by year founded (X axis) and market capitalization (Y axis) with fill determined by vendor name.
Doing this:
qplot(factor(Founded), Market.Cap, data = mcap, geom = "bar", fill = Vendor)
Gets me that, but the X-axis is illegible (see below). I've tried to feed scale_x_discrete() values (1900 - 2012, etc) but had no luck. How do I tell ggplot2 not to display the actual values but instead a spectrum of my choosing?
And as a side note, if you can remind me how to avoid the scientific notation on the Y-axis, great.
I hope this helps. If rotating the axes doesn't work, then you can manually set the breaks and labels of the axis using breaks
and labels
within scale_x_continuous
as follows:
require(ggplot2)
# dummy data
set.seed(45)
len <- 50
df <- data.frame(years = factor(seq(1901, 1950, length.out = len)),
values = 1e6 * runif(len), group=factor(rep(1:5, each=len/5)))
p <- ggplot(data = df, aes(x = years, fill=group)) + geom_bar(aes(weight = values))
require(scales) # for removing scientific notation
p <- p + scale_y_continuous(labels = comma)
# manually generate breaks/labels
labels <- seq(1901, 2000, length.out=10)
# and set breaks and labels
p <- p + scale_x_discrete(breaks=labels, labels=as.character(labels))
p
这篇关于溢出X轴(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!