日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

d3js绘制y坐标轴_【ggplot2】 设置坐标轴

發布時間:2025/3/15 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 d3js绘制y坐标轴_【ggplot2】 设置坐标轴 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基本箱線圖

library(ggplot2)

bp ggplot(PlantGrowth, aes(x=group, y=weight)) +

geom_boxplot()

bp

反轉 x軸 與 y軸

bp + coord_flip()

離散型數據的坐標軸

改變坐標軸中各項目的順序 > 特別注意, 離散數據的坐標軸中數據做為 factor 變量處理,他的位置取決于 level的順序

# 手動設置x軸的位置

bp + scale_x_discrete(limits=c("trt1","trt2","ctrl"))

# 逆轉順序

# 得到 factor 變量的 level

flevels levels(PlantGrowth$group)

flevels

## [1] "ctrl" "trt1" "trt2"

# 逆轉了 level 的順序

flevels rev(flevels)

flevels

## [1] "trt2" "trt1" "ctrl"bp + scale_x_discrete(limits=flevels)

# 或者寫到一行里面

bp + scale_x_discrete(limits = rev(levels(PlantGrowth$group)))

scale_x_discrete 可以設置離散型(discrete)數據, 中間的 x 表示處理x軸,如果是 fill 則可以修改填充顏色, color 修改邊框顏色, shape 修改形狀……

設置坐標軸的標簽

# 將原有的 "ctrl", "trt1", "trt2" 修改為 "Control", "Treat 1", "Treat 2"

bp + scale_x_discrete(breaks=c("ctrl", "trt1", "trt2"),

labels=c("Control", "Treat 1", "Treat 2"))

# 隱藏

bp + scale_x_discrete(breaks=NULL)

# 也可以這樣通過設置 theme 實現

bp + theme(axis.ticks = element_blank(), axis.text.x = element_blank())

連續型數據的坐標軸

設置坐標軸的范圍和顛倒

# Make sure to include 0 in the y axis

bp + expand_limits(y=0) # y軸從0開始

# 設置y軸的范圍

bp + expand_limits(y=c(0,8))

我們可以通過expand_limits設置坐標軸的范圍, 但是如果?scale_y_continuous?被使用, 那么就會覆蓋ylim的設置.

# 設置y軸的范圍

bp + ylim(0, 8)

# 同樣可以這樣

bp + scale_y_continuous(limits=c(0, 8))

# 如果同時設置 scale_y_continuous 和 ylim那么ylim會被覆蓋,首先執行scale_y_continuous

bp + scale_y_continuous(limits=c(0, 8))+

ylim(0,10)

## Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.

如果 y 的范圍使用上面的方法被截取, 那么這個范圍以外的數據會被忽略,原始數據中的數據同樣會被忽略,比如設置了ylim(5,8),那么小于5和大于8的原始數據同樣會被忽略,當然散點圖沒有問題,但是箱線圖會出錯.

為了避免這個問題可以使用coord_cartesian來設置范圍.

可以看下面的例子, 第一個出錯了, 第二個使用了coord_cartesian得到了正確的繪圖.

# These two do the same thing; all data points outside the graphing range are

# dropped, resulting in a misleading box plot

bp + ylim(5, 7.5)

## Warning: Removed 13 rows containing non-finite values (stat_boxplot).

# bp + scale_y_continuous(limits=c(5, 7.5))

# Using coord_cartesian "zooms" into the area

bp + coord_cartesian(ylim=c(5, 7.5))

# Specify tick marks directly

bp + coord_cartesian(ylim=c(5, 7.5)) +

scale_y_continuous(breaks=seq(0, 10, 0.25)) # Ticks from 0-10, every .25

?### 點到坐標軸的方向

# Reverse order of a continuous-valued axis

bp + scale_y_reverse()

設置和隱藏坐標軸的刻度

# Setting the tick marks on an axis

# 顯示刻度從1到10,間隔為0.25

# The scale will show only the ones that are within range (3.50-6.25 in this case)

bp + scale_y_continuous(breaks=seq(1,10,1/4))

# 未設置刻度的地方會出現空白

bp + scale_y_continuous(breaks=c(4, 4.25, 4.5, 5, 6,8))

# 隱藏刻度

bp + scale_y_continuous(breaks=NULL)

# 隱藏刻度但是顯示標簽

bp + theme(axis.ticks = element_blank())

坐標軸的數據轉換(log, sqrt, etc.)

坐標軸可以進行線性變換,比如 log, power, roots 等等

這里有兩種方式對數據進行轉換, 一種是比例轉換, 另一種是坐標轉換. 對于比例變換, 在坐標軸刻度和范圍被決定之前發生變換, 也就是先繪制圖形,在標明刻度; 對于坐標變換, 在坐標軸刻度和范圍被決定之后發生變換.也就是先標明刻度再繪制圖形.

具體的理解可以看下面的例子.

# 指數分布的數據

set.seed(201)

n 100

dat data.frame(

xval = (1:n+rnorm(n,sd=5))/20,

yval = 2*2^((1:n+rnorm(n,sd=5))/20)

)

# 散點圖

sp ggplot(dat, aes(xval, yval)) + geom_point()

sp

# log2 scaling of the y axis (with visually-equal spacing)

library(scales) # Need the scales package

##

## Attaching package: 'scales'

## The following objects are masked from 'package:readr':

##

## col_factor, col_numeric

# 比例變換 scale transformation

sp + scale_y_continuous(trans=log2_trans())

# 坐標變換

sp + coord_trans(y="log2")

# 設置刻度和標簽

sp + scale_y_continuous(trans = log2_trans(),

breaks = trans_breaks("log2", function(x) 2^x), # 這里很有意思,可以著重看一下幫助文檔

labels = trans_format("log2", math_format(2^.x)))

這里還有很多其他的變換, 可以??trans_new?查看幫助

set.seed(205)

n 100

dat10 data.frame(

xval = (1:n+rnorm(n,sd=5))/20,

yval = 10*10^((1:n+rnorm(n,sd=5))/20)

)

sp10 ggplot(dat10, aes(xval, yval)) + geom_point()

# log10

sp10 + scale_y_log10()

# 根據 log10 設置 刻度

sp10 + scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),

labels = trans_format("log10", math_format(10^.x)))

Fixed ratio between x and y axes ### 修改 x 和 y 的比例

# Data where x ranges from 0-10, y ranges from 0-30

set.seed(202)

dat data.frame(

xval = runif(40,0,10),

yval = runif(40,0,30)

)

sp ggplot(dat, aes(xval, yval)) + geom_point()

# 設置為x:y = 1:1

sp + coord_fixed()

# x:y = 1:3

sp + coord_fixed(ratio=1/3)

坐標軸標簽的格式

設置和隱藏坐標標題

bp + theme(axis.title.x = element_blank()) + # Remove x-axis label

ylab("Weight (Kg)") # Set y-axis label

# 另一種方法

bp + scale_x_discrete(name="") +

scale_y_continuous(name="Weight (Kg)")

改變字體和旋轉 刻度標簽

element_text?可以設置文本的格式

# Change font options:

# X-axis label: bold, red, and 20 points

# X-axis tick marks: rotate 90 degrees CCW, move to the left a bit (using vjust,

# since the labels are rotated), and 16 points

bp + theme(axis.title.x = element_text(face="bold", colour="#990000", size=20),

axis.text.x = element_text(angle=90,# 設置旋轉的角度

vjust=0.5,# 設置縱向便宜距離 hjust為橫向偏移距離

size=16) # 字體的大小

)

刻度標簽的格式化

# Label formatters

library(scales) # Need the scales package

bp + scale_y_continuous(labels=percent) + # 顯示百分比

scale_x_discrete(labels=abbreviate) #沒有效果

對于連續型數據的格式化包括 comma, percent, dollar 和科學計數法 對于離散型數據的格式化, abbreviate(縮略詞) 將會去除元音和空格,對于日期可以使用?date_format

當然也可以創作自己的格式, 比如 HH:MM:SS 時間格式

# Self-defined formatting function for times.

timeHMS_formatter function(x) {

h floor(x/60)

m floor(x %% 60)

s round(60*(x %% 1)) # Round to nearest second

lab sprintf('%02d:%02d:%02d', h, m, s) # Format the strings as HH:MM:SS

lab gsub('^00:', '', lab) # Remove leading 00: if present

lab gsub('^0', '', lab) # Remove leading 0 if present

# 返回 lab

}

bp + scale_y_continuous(label=timeHMS_formatter)

?

隱藏參考線

# 隱藏所有參考線(minor, major)

bp + theme(panel.grid.minor=element_blank(),

panel.grid.major=element_blank())

# 隱藏 minor

bp + theme(panel.grid.minor=element_blank())

?

根據坐標軸的方向隱藏

# 隱藏 縱向

bp + theme(panel.grid.minor.x=element_blank(),

panel.grid.major.x=element_blank())

# 隱藏 橫向

bp + theme(panel.grid.minor.y=element_blank(),

panel.grid.major.y=element_blank())

總結

以上是生活随笔為你收集整理的d3js绘制y坐标轴_【ggplot2】 设置坐标轴的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。