我是R的新手,所以如果这是一个非常基本的问题,我会提前道歉。
我正在尝试绘制一个图表,显示流量和悬浮泥沙负荷(SSL)。但是,我想弄清楚条形图表示放电,而线图表示SSL。我有两个想法:
为放电标签和SSL分别着色以分别对应于条形图和折线图,因此读者可以直观地知道哪个标签属于哪个标签。但是ggplot2不允许我这样做,因为它会使两个y轴的颜色都相同。
建立一个图例,清楚地表明红线属于SSL,蓝盒图属于排放。 here上有一篇类似的文章,但我似乎无法实现。如果有人可以帮助我,我将不胜感激。
这是我的图形现在的样子。
这是我的脚本:
library(ggplot2)
library(gridExtra)
library(RColorBrewer)
library(tibble)
P_Discharge <- Pyay$Mean.monthly.discharge
P_MaxTemp <- Pyay$Mean.monthly.max.temperature
P_MinTemp <- Pyay$Mean.monthly.minimum.temperature
P_Rain <- Pyay$Max.monthly.rainfall
P_SSL <- Pyay$Mean.suspended.sediment.load
#reorderingthemonths
Pyay$Month <- factor(Pyay$Month,
levels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
#PlottingdischargeandSSL
Pgraph1 <- ggplot(Pyay, aes(x=Month, group=2))
Pgraph1 <- Pgraph1 + geom_bar(aes(y=P_Discharge), stat="identity", fill="blue")
Pgraph1 <- Pgraph1 + geom_line(aes(y=P_SSL), colour="red", size=1) +
labs(y=expression(Q/(m^{3}))) +
labs(x="")
#addingsecondaxis
Pgraph1 <- Pgraph1 + scale_y_continuous(sec.axis=sec_axis(~., name=expression(
Suspended~sediment~load~(10^{6}~t))))
#colouringaxistitles
Pgraph1 <- Pgraph1 + theme(axis.title.x=element_blank(),
axis.title.y=element_text(size=14),
axis.text.x=element_text(size=14))
Pgraph1
数据
Pyay <- tibble::tribble(
~Month, ~Mean.monthly.discharge, ~Mean.monthly.max.temperature, ~Mean.suspended.sediment.load, ~Max.monthly.rainfall, ~Mean.monthly.minimum.temperature,
"Jan", 8.528, 32.2, 3.407, 1.5, 16.2,
"Feb", 6.316, 35.1, 2.319, 0.9, 17.8,
"Mar", 7, 37.6, 2.587, 5.1, 21.2,
"Apr", 8.635, 38.7, 3.573, 27.3, 24.7,
"May", 12.184, 36, 5.785, 145.1, 25.6,
"Jun", 30.414, 31.9, 21.811, 234.8, 24.8,
"Jul", 70.753, 31, 70.175, 198, 24.8,
"Aug", 79.255, 31, 81.873, 227.5, 24.7,
"Sep", 67.079, 32.3, 65.798, 205.7, 24.6,
"Oct", 53.677, 33.5, 47.404, 124, 24.2,
"Nov", 22.937, 32.7, 14.468, 56, 21.7,
"Dec", 12.409, 31.5, 5.842, 1.5, 18.1
)
答案 0 :(得分:4)
如果将fill
和aes()
参数放在scale_fill_manual
内,则会得到一个图例。使用color
,我们将条形更改为蓝色。将fill
和labs()
""
设置为## Plotting discharge and SSL
Pgraph1 <- ggplot(Pyay, aes(x=Month, group = 2))
Pgraph1 <- Pgraph1 + geom_bar(aes(y=P_Discharge, fill = "discharge"), stat="identity")
Pgraph1 <- Pgraph1 + geom_line(aes(y=P_SSL, colour = "SSL"), size=1)+ labs(y=expression(Q/(m^{3}))) + labs(x=" ")
Pgraph1 <- Pgraph1 + scale_fill_manual(values = c("discharge" = "blue")) + labs(color = "", fill = "")
#adding second axis
Pgraph1 <- Pgraph1 + scale_y_continuous(sec.axis = sec_axis(~.,name = expression(Suspended~sediment~load~(10^{6}~t))))
#colouring axis titles
Pgraph1 <- Pgraph1 + theme(
axis.title.x = element_blank(),
axis.title.y = element_text(size=14),
axis.text.x = element_text(size=14)
)
Pgraph1
会删除它们。
function lowerCase(ans) {
if(!ans){ //Fail safe when empty string sent
return '';
}
var lowCase = ans.toLowerCase();
return lowCase; // changed returned value to lowCase earlier you were returning 'lowerCase'
}
var questions = ['How may strings does a violin have?', 'How many sides does an octagon have?',
'How many NBA championships did Michael Jordan win with the Chicago Bulls?'
];
var answers = ['FOUR', 'EIGHT', 'SIX'];
var score = 0;
function quiz(counter) {
var guesses = 1;
while (guesses > 0) {
var ans = prompt(questions[counter]);
ans = lowerCase(ans); // Lower case conversion done separately for clarity
document.write('Guess: '+ans+'</br>'); // Moved the document write to a place where answer is visible
if (ans === lowerCase(answers[counter])) { // the answer array values were also converted to lower case if not this logic is broken
alert("Correct!");
return guesses;
} else {
guesses--;
alert("Incorrect, You have " + guesses + " guesses remaining");
}
}
return 0;
}
quiz(0); // called quiz with zero as an example
答案 1 :(得分:3)
您还可以考虑基准R图,我发现它更简单一些。
par(mar=c(5, 5, 4, 5) + 0.1) # adjust plot margins
b <- barplot(Pyay$Mean.monthly.discharge, col="blue", # plots and saves x-coordinates
ylim=c(0, 90),
ylab=expression(Q/(m^{3})))
lines(b, Pyay$Mean.suspended.sediment.load, col="red", lwd=2) # use x-coordinates here
axis(1, b, labels=Pyay$Month)
axis(4, seq(0, 90, 20), labels=, seq(0, 90, 20))
mtext(expression(Suspended~sediment~load~(10^{6}~t)), 4, 3)
legend("topleft", legend=c("discharge", "SSL"), pch=c(15, NA),
pt.cex=2, lty=c(0, 1), col=c("blue", "red"))
box()
数据
Pyay <- structure(list(Month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), Mean.monthly.discharge = c(8.528,
6.316, 7, 8.635, 12.184, 30.414, 70.753, 79.255, 67.079, 53.677,
22.937, 12.409), Mean.monthly.max.temperature = c(32.2, 35.1,
37.6, 38.7, 36, 31.9, 31, 31, 32.3, 33.5, 32.7, 31.5), Mean.suspended.sediment.load = c(3.407,
2.319, 2.587, 3.573, 5.785, 21.811, 70.175, 81.873, 65.798, 47.404,
14.468, 5.842), Max.monthly.rainfall = c(1.5, 0.9, 5.1, 27.3,
145.1, 234.8, 198, 227.5, 205.7, 124, 56, 1.5), Mean.monthly.minimum.temperature = c(16.2,
17.8, 21.2, 24.7, 25.6, 24.8, 24.8, 24.7, 24.6, 24.2, 21.7, 18.1
)), row.names = c(NA, -12L), class = "data.frame")