R ggplot focusing in region and different scale in

2019-08-20 03:00发布

问题:

I googled but can't find an answer to my problem. I have some dates:

  • date for temperature (region at 30 to 60 gr Celsia)
  • date for consumption in Watt (region at 100 to 160 Watt)
  • date for frequency in GHz (region at 1.2 to 3.1 GHz)

Then I plot these dates in one graphic I cant see different in frequency (it so small).

How add to axis ticks like 1.2 to 3.1 with step 0.1 and after return to step 10?

And how to add scale for region like 1.2 to 3.1 (it will be so big as step with 10 else I cant see. Step ticks it so small) and after return to scale normal for step 10 ?

I wont focusing in some regions of axis add some scale for focused region.

P.S. I can do this in gnuplot if set multi axis but I want to try it in R.

P.S. P.S. sorry for English

library(ggplot2)
s=matrix(0,10,4);
s[1:10,1]=1:10;
s[1:10,2]=1.2:3.1;
s[1:10,3]=70:79;
s[1:10,4]=150:159;
colnames(s) = c("time", "freq", "temp", "power");
ggplot(data=NULL, aes(x=s[,1], ) ) +
    geom_line( aes(y=s[,3], color="blue") )  +
    geom_line( aes(y=s[,4], color="red") )  +
    geom_line( aes(y=s[,2], color="green") ) +
    scale_y_continuous(breaks=c(1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.0,3.1,4,6,8,10) );

回答1:

The 'problem' with your data is that the freq, temp & power variables have very different scales. As a result the variation in especially the freq variable can hardly be seen when you combine them all in one plot. Moreover, for the other two variables the development over time is hard to see as weel.

In this case it's probably wiser to use a faceted plot:

# transforming the data into a dataframe of the long format
df <- as.data.frame(s)
require(reshape2)
df2 <- melt(df, id="time")

# creating the plot
ggplot(df2, aes(x=time, y=value, color=variable)) +
  geom_line() +
  facet_grid(variable ~., scales="free_y")

the result:



标签: r ggplot2 scale