我正在制作一系列不同股票的时间序列,并且我在绘制它们时遇到了一些问题。
所以我的数据集看起来像这样:
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.practice.AppConstant;
public class AuthenticationTokenFilter extends UsernamePasswordAuthenticationFilter {
private Tokenutils tokenUtils;
//@Autowired
//private UserDetailsService userDetailService;
@Override
public void doFilter(ServletRequest request , ServletResponse res , FilterChain chain) throws IOException , ServletException{
tokenUtils=(Tokenutils) WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext()).getBean(Tokenutils.class);
//userDetailService=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext()).getBean(UserDetailsService.class);
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
//response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,"+AppConstant.tokenHeader);
HttpServletRequest httpRequest = (HttpServletRequest)request;
//String authToken=httpRequest.getHeader(AppConstant.tokenHeader);
String authToken=httpRequest.getParameter("XAuthToken");
String userName=this.tokenUtils.getUsernameFromToken(authToken);
if(userName!=null && SecurityContextHolder.getContext().getAuthentication()==null){
//UserDetails userDetails= this.userDetailService.loadUserByUsername(userName);
UserDetails userDetails=new SpringSecurityUser(1L, userName, null, null, null, AuthorityUtils.commaSeparatedStringToAuthorityList(""));
if(this.tokenUtils.validateToken(authToken,userDetails)){
UsernamePasswordAuthenticationToken authentication= new UsernamePasswordAuthenticationToken(userDetails, null,userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
chain.doFilter(request, res);
}
}
现在,我手动绘制了所有 A B C D
1/2/2012 0.007 0.012 0.015 0.009
1/3/2012 0.009 0.012 0.015 0.008
1/4/2012 0.012 0.012 0.015 0.009
1/5/2012 0.013 0.012 0.015 0.012
1/6/2012 0.013 0.012 0.015 0.011
1/9/2012 0.013 0.012 0.015 0.011
1/10/2012 0.013 0.009 0.015 0.011
1/11/2012 0.013 0.009 0.015 0.014
1/12/2012 0.013 0.009 0.015 0.014
1/13/2012 0.013 0.009 0.015 0.013
1/16/2012 0.013 0.012 0.014 0.017
1/17/2012 0.013 0.013 0.015 0.017
1/18/2012 0.014 0.013 0.015 0.018
1/19/2012 0.014 0.013 0.015 0.018
1/20/2012 0.015 0.012 0.015 0.018
1/24/2012 0.016 0.011 0.016 0.018
1/25/2012 0.016 0.011 0.016 0.019
1/26/2012 0.016 0.010 0.015 0.021
1/27/2012 0.016 0.010 0.015 0.022
1/30/2012 0.016 0.010 0.015 0.022
1/31/2012 0.016 0.010 0.015 0.022
2/1/2012 0.016 0.010 0.015 0.022
2/2/2012 0.020 0.012 0.015 0.025
并通过gX <- ggplot(dat, aes(Index, X)) + geom_line
有没有办法使用facet_grid绘制这些图,因为我这样做是低效和僵硬的?
以下是我手动完成的情节:
答案 0 :(得分:2)
假设您的data.frame A
,B
,C
和D
是股票名称,您必须先创建以下格式的长表:
Date Stock value
1/2/2012 A 0.007
1/2/2012 B 0.012
1/2/2012 C 0.015
1/2/2012 D 0.009
1/3/2012 A 0.009
1/3/2012 A 0.012
1/3/2012 A 0.015
1/3/2012 A 0.008
我假设您的第一列是rownames,并且必须将其作为列导入数据框。然后使用gather
中的tidyr
以长格式创建数据框。
library(tidyr)
df$Date <- rownames(df)
long <- gather(df, Stock, value, -Date)
ggplot(aes(x=Date, y=Value)) + geom_line() + facet_wrap(~Stock, scales='free_y', ncol=5)
在gather
中,第一个参数第二个参数是包含早期列名的列的名称,第三个参数是包含其值的列的名称。 gather
的以下参数是选择要收集的列,在这种情况下,除了 Date
之外它都是(因为你有很多并且不想打扰手动输入所有内容)。