简体   繁体   English

Y 轴标签丢失?

[英]Y-axis Labels Missing?

the plot I am working on works fine but for some reason, the Y-Axis label is not showing.我正在处理的绘图工作正常,但由于某种原因,Y 轴标签没有显示。 Any help would be appreciated.任何帮助,将不胜感激。

Europe <- subset(data, continent == "Europe")
Americas<- subset(data, continent == "Americas")
Asia <- subset(data, continent == "Asia")

counts_europe <- table(Europe$decade)
counts_americas <- table(Americas$decade)
counts_asia <- table(Asia$decade)

plot(names(counts_europe), counts_europe , col = "blue", type = "l",
main = "Number of Assassinations by Decade", xlab = "Decade",
ylab = "Number of Assassinations", xlim = c(1875, 2005))

lines(names(counts_americas), counts_americas, col = "red", type = "l")
lines(names(counts_asia), counts_asia , col = "green", type = "l")

text(1935, 13, "Europe")
text(1935, 8, "Americas")
text(1935, 5, "Asia")

This is a very weird situation, basically, you have on the x variable a character and y variable a table , so I think the plot function got confused.这是一个非常奇怪的情况,基本上,你在 x 变量上有一个character ,在 y 变量上有一个table ,所以我认为 plot 函数被混淆了。 So for example:例如:

set.seed(100)
counts_europe = table(sample(1880:1980,200,replace=TRUE))
class(names(counts_europe))
[1] "character"
plot(names(counts_europe),counts_europe,type="l")

在此处输入图片说明

You need to make both of them numeric, one way is below:您需要将它们都设为数字,一种方法如下:

res = cbind(year=as.numeric(names(counts_europe)),counts_europe)
plot(res,type="l")

在此处输入图片说明

I'll take a stab, since we don't have your data.我会试一试,因为我们没有你的数据。

Up front, Y axis labels/ticks are generally automatic.在前面,Y 轴标签/刻度通常是自动的。

For most of par options, you can also enable them within the call to plot and get the effects temporarily for that instance of plot .对于大多数par选项,您还可以在调用plot启用它们,并临时获得该plot实例的效果。 That's true here:这是真的:

plot(1:3, 2:4, type='l', main='Quux', yaxt = 'n')

基本情节,没有y-ticks

However, as it is temporary, a subsequent call to plot without yaxt does not carry forward that option.但是,由于它是暂时的,因此在没有yaxt情况下对plot的后续调用不会继承该选项。

For many, however, including those extra 8 characters (10+ with spaces, comma) is onerous, so we have the ability to set that option as the default for all subsequent plots on that particular graphics device :然而,对于许多人来说,包括那些额外的 8 个字符(10+ 带空格、逗号)是繁重的,所以我们有能力将该选项设置为该特定图形设备上所有后续绘图的默认值:

par(yaxt = "n")
# any number of other plotting things going on here
plot(1:3, 2:4, type='l', main='Quux')

(Same plot, no y-axis ticks.) (相同的图,没有 y 轴刻度。)

There are two approaches to fixing this:有两种方法可以解决这个问题:

  1. Manually reset the option with par(yaxt = "s") (its default value).使用par(yaxt = "s") (其默认值)手动重置该选项。 This has the unfortunate property that it resets only that parameters ... since you already know you have one stale option, it's possible you have others that you don't know about.这有一个不幸的特性,它只重置那些参数......因为你已经知道你有一个陈旧的选项,你可能还有其他你不知道的选项。

  2. Close the graphics device and start over.关闭图形设备并重新开始。 This is easily done with dev.off , though please ensure you are closing the correct device (I occasionally have multiple graphics open, and have closed the wrong one ...).这很容易使用dev.off完成,但请确保您关闭了正确的设备(我偶尔会打开多个图形,并且关闭了错误的一个...)。

    In base R (where a new window appears for each plot device), it is visible in the window title, as in在基础 R(每个绘图设备出现一个新窗口)中,它在窗口标题中可见,如

    R 图形设备窗口

    and is confirmed programmatically on the console with:并在控制台上以编程方式确认:

     dev.list() # windows # 2 dev.off(2)

    RStudio is not that different, and though there is no device number in its UI, one can find: RStudio 没有什么不同,虽然它的 UI 中没有设备编号,但可以找到:

     dev.list() # RStudioGD png # 2 3 dev.off(2)

    When you call dev.off(2) , the graphics pane in the UI will go blank, after which a subsequent plot will bring it back to its original state (hopefully, with yaxt returned to its default value along with any other values that may have changed).当您调用dev.off(2) ,UI 中的图形窗格将dev.off(2)空白,之后后续绘图会将其恢复到其原始状态(希望yaxt以及任何其他可能已经变化)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM