有人知道如何在R中使用ggplot2制作这样的图表吗?

有人知道如何在R中使用ggplot2制作这样的图表吗?,r,ggplot2,stock,R,Ggplot2,Stock,查看我试图在R中使用ggplot2创建的图。该图显示了我的示例数据以及我希望图表的外观和功能。有什么想法吗 以下是我的尝试: #sample data library( data.table ) DT <- data.table::fread("Ticker Name Purchase Current Target Action FB Facebook 16 45 42 Sell AAPL Apple 5 2 22 Buy GOOG Google 21 32 42 Hol

查看我试图在R中使用ggplot2创建的图。该图显示了我的示例数据以及我希望图表的外观和功能。有什么想法吗

以下是我的尝试:

#sample data
library( data.table )
DT <- data.table::fread("Ticker Name Purchase Current Target Action
FB   Facebook 16 45 42 Sell
AAPL Apple     5  2 22 Buy
GOOG Google   21 32 42 Hold")
以下是我的尝试:

#sample data
library( data.table )
DT <- data.table::fread("Ticker Name Purchase Current Target Action
FB   Facebook 16 45 42 Sell
AAPL Apple     5  2 22 Buy
GOOG Google   21 32 42 Hold")

这里是一个近距离复制:



df这是一个近似复制:



df感谢@AllanCameron和@Wimpel的贡献。最后,我把你的两个回答混合在一起,以符合我的要求。下面是在KNIME中使用R视图(表)节点实现的内容

库(data.table)

DT感谢@AllanCameron和@Wimpel的贡献。最后,我把你的两个回答混合在一起,以符合我的要求。下面是在KNIME中使用R视图(表)节点实现的内容

库(data.table)

DT请展示您已经尝试过的内容(以及失败的内容)。。。另外,使用
dput()
的一些数据也会有所帮助……这里是一个粗略的尝试。#股票数据df请显示您已经尝试过的(以及失败的)。。。此外,使用
dput()
的一些数据可能会有所帮助……这里是一个粗略的尝试。#这方面的股票数据看起来不错,但如果我添加另一个具有类似“Action”值的记录,我会得到一个错误“error:factor level[2]重复”……例如,如果我有两支股票处于“Hold”状态。有什么想法吗?这看起来不错,但如果我添加另一个具有类似“动作”值的记录,我会得到一个错误“错误:因子级别[2]重复”…例如,如果我有两支股票处于“持有”状态。有什么想法吗?这真的很好-我喜欢基于最大y坐标的动态标签。最后,我为您提取了代码片段和另一个响应中的代码片段,以创建我想要的内容。我将发布在KNIME中实现的最终代码。这真的很好-我喜欢基于最大y坐标的动态标签。最后,我为您提取了代码片段和另一个响应中的代码片段,以创建我想要的内容。我将发布在KNIME中实现的最终代码。

df <- data.frame( Ticker = c('FB','AAPL','GOOG'), 
                  Name = c('Facebook','Apple','Google'), 
                  Purchase = c(16,5,21), 
                  Current = c(45,2,32), 
                  Target = c(42,22,42), 
                  Action = c('Sell','Buy','Hold') )

df$Ticker <- factor(df$Ticker, levels = as.character(df$Ticker))
df$Action <- factor(df$Action, levels = as.character(df$Action))

ggplot(df, aes(Ticker, Current)) + 
  geom_boxplot(aes(ymin = Purchase, middle = Purchase, lower = Purchase,
                   upper = Target, ymax = Target, fill = Action),
               stat = "identity", size = 0) + 
  scale_fill_manual(values = c("#EE8800", "#6688FF", "#EEDD00"), guide = FALSE) +
  scale_y_continuous(labels = function(x) paste0("$", x)) +
  geom_point(size = 10) +
  geom_point(size = 9, colour = "forestgreen") +
  scale_x_discrete() +
  theme_bw() +
  theme(panel.grid = element_blank(),
        panel.border  = element_rect(size=2),
        axis.title = element_blank(),
        text = element_text(size = 16),
        plot.margin = margin(50, 20, 20, 20)) +
  coord_cartesian(clip = "off", ylim = c(0, 45)) +
  geom_text(aes(y = 50, label = Action), size = 5)
library(data.table)
DT <- data.table::setDT(knime.in)

#add rownumbers
DT[, rownum := .I ]
#make ticker a factor to avoid reordering
DT[, Ticker_f := factor(Ticker, levels = Ticker) ]
#determine maximum y-value
max_y_value = 10 * ceiling( max( matrixStats::colMaxs( as.matrix( DT[, .(Purchase, 
Current, Target ) ] ) ) ) / 10 )

#build chart
library( ggplot2 )
library( scales )
#plot using Name as fill-color, and Ticker for x-axis labels
plot3 <- ggplot( data = DT, aes( x = Ticker_f, fill = Ticker ) ) +
  #create the rectangles
  geom_rect( aes( ymin = Purchase, ymax = Target ), xmin = DT$rownum - 0.2, xmax = 
DT$rownum + 0.2, color = "black" ) + 
  #set colors
  scale_fill_manual(values = c("#737373", "#4472C4", "#ED7D31", "#FFC000"), guide 
= FALSE) +
  #draw points
  geom_point( aes( y = Current ), colour = "#70AD47", size = 10 ) + 
  #show action on top
  geom_text( aes( y = max_y_value, label = Action ), size = 5 ) + 
  #set labels lfor y-axis
  scale_y_continuous( labels = scales::label_dollar() ) + 
  #set theme
  theme_classic() + 
  #zoom to relevant values
  coord_cartesian( ylim = c(0, max_y_value ) ) + 
  #
  ggtitle("Current Status of Portfolio") +
  xlab("Tickers") + 
  ylab("Price") +
  theme(
    plot.title = element_text(color="gray", size=14, face="bold.italic"),
    axis.title.x = element_text(color="gray", size=14, face="bold"),
    axis.title.y = element_text(color="gray", size=14, face="bold"),
    axis.text.x = element_text(size = 14),
    axis.text.y = element_text(size = 14))


plot(plot3)