在ggplot2中,我如何阻止轴标签被缩写 - 例如,在绘制时沿着x轴显示
1e + 00,1e + 01
?理想情况下,我希望强制R显示实际值,即在此情况下为1,10
。任何帮助将不胜感激。我想你正在寻找这个:
require(ggplot2)
df <- data.frame(x=seq(1, 1e9, length.out=100), y=sample(100))
# displays x-axis in scientific notation
p <- ggplot(data = df, aes(x=x, y=y)) + geom_line() + geom_point()
p
# displays as you require
library(scales)
p + scale_x_continuous(labels = label_comma())
你尝试过类似以下的内容吗:
options(scipen=10000)
绘图之前?
就@Arun的最新消息更新一下,因为我今天尝试了一下,但它没有起作用,因为它已被实现到了
+ scale_x_continuous(labels = scales::comma)
require(scales)
了?这会导入包含“逗号”刻度的软件包。正如您已经发现的那样,您还可以在引用软件包时指定它,而不必事先要求它。 - cincodenada作为一种更通用的解决方案,您可以使用 scales::format_format
来移除科学计数法。这还可以让您对标签的准确显示方式拥有更多的控制,而不像 scales::comma
只能在数量级之间进行逗号分隔。
例如:
require(ggplot2)
require(scales)
df <- data.frame(x=seq(1, 1e9, length.out=100), y=sample(100))
# Here we define spaces as the big separator
point <- format_format(big.mark = " ", decimal.mark = ",", scientific = FALSE)
# Plot it
p <- ggplot(data = df, aes(x=x, y=y)) + geom_line() + geom_point()
p + scale_x_continuous(labels = point)
有一种方法不需要使用scales库。
你可以尝试以下操作:
# To deactivate scientific notation on y-axis:
p + scale_y_continuous(labels = function(x) format(x, scientific = FALSE))
# To activate scientific notation on y-axis:
p + scale_y_continuous(labels = function(x) format(x, scientific = TRUE))
# To deactivate scientific notation on x-axis:
p + scale_x_continuous(labels = function(x) format(x, scientific = FALSE))
# To activate scientific notation on x-axis:
p + scale_x_continuous(labels = function(x) format(x, scientific = TRUE))
p + scale_x_continuous(labels = function(x) sprintf("%g", x))
p + scale_x_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE))
p + scale_x_continuous(labels = scales::number_format(accuracy = 1))
accuracy = 1
表示整数的精度,如果你想要一位小数,可以使用 accuracy = 0.1
,如果需要两位小数,可以使用accuracy = 0.01
,以此类推。
scipen()
设置为您感到舒适的数字。例如,如果您的图表坐标轴最大值可能达到 100000,设置 scipen(200000)
将确保 R(和 ggplot)对所有低于 200000 的数字使用标准表示法,并且不需要向 ggplot 函数添加任何行。import socket
# 创建服务器套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定服务器地址和端口
server_socket.bind(('localhost', 8080))
# 开始监听传入连接
server_socket.listen()
print('Server listening on port 8080...')
while True:
# 接受客户端连接
client_socket, address = server_socket.accept()
print(f'Accepted connection from {address}')
# 接收客户端数据
data = client_socket.recv(1024)
print(f'Received data from client: {data.decode()}')
# 发送响应给客户端
response = 'Hello from the server!'
client_socket.send(response.encode())
# 关闭客户端套接字
client_socket.close()
要测试此服务器,请运行上面的代码并使用telnet或类似工具连接到localhost:8080。您应该能够看到服务器打印出已接受连接并从客户端接收数据。 - tjebolibrary(scales)
ggplot(data, aes(salary)) +
geom_histogram() +
scale_x_continuous(labels = comma)
在这里,scale_x_continuous(labels = comma)
可以解决这个问题。
scales::comma
是scales::label_comma
的简写,等等。 - tjebo错误:中断和标签长度不同
。 - Joshua Eric Turcottelibrary(scales)
。 - George Hayward