我需要绘制一个显示计数的条形图和一个在一个图表中显示速率的折线图,我可以分别做两个,但是当我把它们放在一起时,我的第一层的比例(即geom_bar)与第二层重叠层(即geom_line).
我可以将轴geom_line向右移动吗?
我正在创建一个图表,显示湖水位随时间的变化.我在下面附上了一个简单的例子.我想在图的右侧添加一个刻度(刻度线和注释),以英尺为单位显示高程.我知道ggplot2不会允许两个不同的比例(参见2 y轴的绘图,左边的一个y轴,右边的另一个y轴),但因为这是相同比例的转换,有没有办法做这个?我更喜欢继续使用ggplot2而不必恢复到plot()函数.
library(ggplot2)
LakeLevels<-data.frame(Day=c(1:365),Elevation=sin(seq(0,2*pi,2*pi/364))*10+100)
p <- ggplot(data=LakeLevels) + geom_line(aes(x=Day,y=Elevation)) +
scale_y_continuous(name="Elevation (m)",limits=c(75,125))
p
Run Code Online (Sandbox Code Playgroud) 我们不能用ggplot2轻松创建双y轴.
如果数字有多个面板(由geom_wrap或geom_grid创建),例如下面的4面板图,则这些技巧无法工作.我想将p1的轴添加到合并图的面板的右侧.
编辑:我在这里删除了我的代码并发布我自己的解决方案作为答案.以防有人需要它.
我正在尝试使用ggplot构建双y轴图表.首先,请允许我说,我不是在讨论是否这样做是好的做法的优点.在查看基于时间的数据以确定2个离散变量的趋势时,我发现它们特别有用.在我看来,对此的进一步讨论更适合交叉验证.
Kohske提供了一个如何做到这一点的非常好的例子,到目前为止我已经习惯了很好的效果.然而,我无法在两个y轴上包含一个图例.我在这里和这里也看到过类似的问题,但似乎没有人能解决包含传奇的问题.
我有一个可重现的例子,使用ggplot中的钻石数据集.
数据
library(ggplot2)
library(gtable)
library(grid)
library(data.table)
library(scales)
grid.newpage()
dt.diamonds <- as.data.table(diamonds)
d1 <- dt.diamonds[,list(revenue = sum(price),
stones = length(price)),
by=clarity]
setkey(d1, clarity)
Run Code Online (Sandbox Code Playgroud)
图表
p1 <- ggplot(d1, aes(x=clarity,y=revenue, fill="#4B92DB")) +
geom_bar(stat="identity") +
labs(x="clarity", y="revenue") +
scale_fill_identity(name="", guide="legend", labels=c("Revenue")) +
scale_y_continuous(labels=dollar, expand=c(0,0)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.text.y = element_text(colour="#4B92DB"),
legend.position="bottom")
p2 <- ggplot(d1, aes(x=clarity, y=stones, colour="red")) +
geom_point(size=6) +
labs(x="", y="number of stones") + expand_limits(y=0) +
scale_y_continuous(labels=comma, expand=c(0,0)) +
scale_colour_manual(name …Run Code Online (Sandbox Code Playgroud) 我想在轴标题旁边添加图例.我按照这个stackoverflow的答案得到了情节.
如何在两个y轴上添加图例?.我想在左右Y轴上都有传奇.在下图中,右侧y轴缺少图例符号.
也可以为类似于图例中的符号的文本提供独特的颜色.
同时,如何将图例键符号旋转到垂直位置?
我到目前为止的代码:
## install ggplot2 as follows:
# install.packages("devtools")
# devtools::install_github("hadley/ggplot2")
packageVersion('ggplot2')
# [1] ‘2.2.0.9000’
packageVersion('data.table')
# [1] ‘1.9.7’
# libraries
library(ggplot2)
library(data.table)
# data
df1 <- structure(list(plate_num = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L),
`Before Treatment` = c(662.253098499674, 684.416067929458, 688.284595300261,
692.532637075718, 728.988910632746, 684.708496732026,
703.390706806283, 673.920966688439, 644.945573770492, 504.423076923077,
580.263743455497, 580.563767168084, 689.6014445174, 804.740789473684,
815.792020928712, 789.234139960759, …Run Code Online (Sandbox Code Playgroud) 我遇到了一个问题,即在第二轴功能的帮助下使用两个不同的数据,如前一篇文章中所述,如何使用-facets-with-a-dual-y-axis-ggplot.
我正在尝试使用geom_point,geom_bar但由于geom_bar数据范围不同,因此在图表中看不到.
这是我尝试过的;
point_data=data.frame(gr=seq(1,10),point_y=rnorm(10,0.25,0.1))
bar_data=data.frame(gr=seq(1,10),bar_y=rnorm(10,5,1))
library(ggplot2)
sec_axis_plot <- ggplot(point_data, aes(y=point_y, x=gr,col="red")) + #Enc vs Wafer
geom_point(size=5.5,alpha=1,stat='identity')+
geom_bar(data=bar_data,aes(x = gr, y = bar_y, fill = gr),stat = "identity") +
scale_y_continuous(sec.axis = sec_axis(trans=~ .*15,
name = 'bar_y',breaks=seq(0,10,0.5)),breaks=seq(0.10,0.5,0.05),limits = c(0.1,0.5),expand=c(0,0))+
facet_wrap(~gr, strip.position = 'bottom',nrow=1)+
theme_bw()
Run Code Online (Sandbox Code Playgroud)
可以看出bar_data被删除了.是否可以在这种情况下将它们一起绘制?
谢谢
对于出版物,我需要在现有图中添加第二个y轴.我遇到过如何做到这一点的方法(https://rpubs.com/kohske/dual_axis_in_ggplot2).但是,我并不太了解编码.我找不到一种方法来使它成为正确的y轴也显示,而不是顶部边框.我的编码中缺少什么?这是我的虚拟数据:
df1 <- structure(list(month = structure(1:12, .Label = c("Apr", "Aug",
"Dec", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Nov", "Oct",
"Sep"), class = "factor"), RI = c(0.52, 0.115, 0.636666666666667,
0.807, 0.66625, 0.34, 0.143333333333333, 0.58375, 0.173333333333333,
0.5, 0.13, 0), sd = c(0.327566787083184, 0.162634559672906, 0.299555225848813,
0.172887246493199, 0.293010848165827, 0.480832611206852, 0.222785397486759,
0.381610777775321, 0.219393102292058, 0.3, 0.183847763108502,
0)), .Names = c("month", "RI", "sd"), class = "data.frame", row.names = c(NA,
-12L))
df2<-structure(list(month = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L), .Label …Run Code Online (Sandbox Code Playgroud) 对于水文学家来说,通常使用降雨水文仪和水流水文仪。如下图所示。

X轴表示日期,左侧的Y轴表示降雨,右侧的Y轴表示流量。
我有一个降雨表和一个排水表。
####Rain Table#### ####Discharge Table####
Date Value Date Value
2000-01-01 13.2 2000-01-01 150
2000-01-02 9.5 2000-01-01 135
2000-01-03 7.3 2000-01-01 58
2000-01-04 0.2 2000-01-01 38
Run Code Online (Sandbox Code Playgroud)
这是我的代码。
ggplot(rain,aes(x=DATE,y=value)) +
geom_bar(stat = 'identity')+
scale_y_reverse()+
geom_line(data =discharge,aes(x=DATE,y=value))
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何在两个不同的Y轴上表示这些值。
我正在使用facet_wrap并且还能够绘制次要y轴.然而,标签没有在轴附近绘制,而是绘制得非常远.我意识到如果我理解如何操纵凹槽的gtable(t,b,l,r)的坐标系,它将全部解决.有人可以解释他们实际描绘的方式和内容 - t:r = c(4,8,4,4)意味着什么.
辅助y轴与ggplot有许多链接,但是当nrow/ncol大于1时,它们会失败.那么请教我网格几何和grobs位置管理的基础知识.
编辑:代码
this is the final code written by me :
library(ggplot2)
library(gtable)
library(grid)
library(data.table)
library(scales)
# Data
diamonds$cut <- sample(letters[1:13], nrow(diamonds), replace = TRUE)
dt.diamonds <- as.data.table(diamonds)
d1 <- dt.diamonds[,list(revenue = sum(price),
stones = length(price)),
by=c("clarity", "cut")]
setkey(d1, clarity, cut)
# The facet_wrap plots
p1 <- ggplot(d1, aes(x = clarity, y = revenue, fill = cut)) +
geom_bar(stat = "identity") +
labs(x = "clarity", y = "revenue") +
facet_wrap( ~ cut) +
scale_y_continuous(labels = …Run Code Online (Sandbox Code Playgroud)